如何在c ++中为同一枚举变量分配多个枚举值?

时间:2017-10-25 12:52:45

标签: c++

例如,我们初始化枚举实例。

class Example { 
 static void main(String[] args) { 
  // Initializing a local variable 
  int a = 2

  //Check for the boolean condition 
  if (a<100 && a>0) { 
     //If the condition is true print the following statement 
     println("The value is less than 100"); 
  } else { 
     //If the condition is false print the following statement 
     println("The value is greater than 100"); 
  } 
 } 
}

我们可以enum weekdays {Saturday, Sunday, Tuesday,Wednesday, Thursday, Friday} weekend; 分配weekend吗?

2 个答案:

答案 0 :(得分:1)

使用相同类型进行枚举和收集日期会非常混乱。您应该将WeekDay区分为日期符号和日期集合,如Weekend中所示。查看int类型。变量int something只能包含一个数字。如果你想要更多,你必须使用集合:array,vector等。

我假设您要创建每周只出现一次的工作日集合。为此,我们通常使用集合。

此答案包含使用std::set,然后是std::bitset和自定义类的示例。

std::set的示例。

#include <iostream>
#include <set>

enum WeekDay { Saturday, Sunday, Monday, Tuesday, Wednesday, Thursday, Friday };

int main()
{
    std::set<WeekDay>   Weekend{ WeekDay::Saturday, WeekDay::Sunday };
    // For older compilers can be replaced with:
    // std::set<WeekDay>    Weekend;
    // Weekend.insert( WeekDay::Saturday );
    // Weekend.insert( WeekDay::Sunday );

    WeekDay today = WeekDay::Wednesday;

    // There is no Weekend.contains( today )
    // nor something likie Pascal
    // today in Weekend

    // Not so pretty
    if ( Weekend.find( today ) != Weekend.end() )
    //alternative:
    //if ( Weekend.count( today ) )
        std::cout << "Weekend :)\n";
    else
        std::cout << "Not weekend :(\n";
    return 0;
}

不幸的是,它看起来不漂亮或优雅。我愿意改进。

std::bitset版本:

#include <iostream>
#include <bitset>

enum WeekDay { Saturday, Sunday, Monday, Tuesday, Wednesday, Thursday, Friday };

int main()
{
    // I don't like this. Number of elements = last element + 1.
    std::bitset<WeekDay::Friday+1>  Weekend;
    Weekend[ WeekDay::Saturday ] = true;
    Weekend[ WeekDay::Sunday ] = true;

    WeekDay today = WeekDay::Wednesday;

    if ( Weekend[ today ] )
        std::cout << "Weekend :)\n";
    else
        std::cout << "Not weekend :(\n";
    return 0;
}

自定义解决方案:

#include <iostream>
#include <assert.h>

enum WeekDay { Saturday, Sunday, Monday, Tuesday, Wednesday, Thursday, Friday };

class WeekDays
{
public:
    WeekDays() : set( 0 ) {}

    // For older compilers just omit this constructor.    
    WeekDays( const std::initializer_list<WeekDay>& init )
        : set( 0 )
    {
        for ( auto it = init.begin() ; it != init.end() ; ++it )
            insert( *it );
    }

    void insert( WeekDay d )
    {
        assert( d >= WeekDay::Saturday && d < WeekDay::Friday && d >= 0 && d <= 31 );
        set |= 1 << d;
    }

    bool contains( WeekDay d )
    {
        assert( d >= WeekDay::Saturday && d < WeekDay::Friday && d >= 0 && d <= 31 );
        return ( set & ( 1 << d ) ) != 0;
    }

private:
    unsigned int    set;
};

int main()
{
    WeekDays    Weekend{ WeekDay::Saturday, WeekDay::Sunday };
    // For older compilers can be replaced with:
    // WeekDays Weekend;
    // Weekend.insert( WeekDay::Saturday );
    // Weekend.insert( WeekDay::Sunday );

    WeekDay     today = WeekDay::Wednesday;
    if ( Weekend.contains( today ) )
        std::cout << "Weekend :)\n";
    else
        std::cout << "Not weekend :(\n";
    return 0;
}

答案 1 :(得分:-1)

你可以宣告你的枚举为2的力量。 对于E.g

enum weekdays {Saturday  =0 , Sunday = 1, Tuesday = 2,Wednesday = 4, Thursday = 8, Friday= 16} weekend;

现在您可以使用|分配多个枚举他们之间的运营商。

要检查是否设置了任何枚举,请使用oprator&amp;