-1

For example,we initialize an instance of enum.

enum weekdays {Saturday, Sunday, Tuesday,Wednesday, Thursday, Friday} weekend;

and can we make weekend assigned both Saturday and sunday

8
  • 1
    Hint: Powers of 2 and bitmasks...
    – Sean
    Oct 25, 2017 at 12:54
  • 1
    Do you understand that enumerator values are just numbers? Which by default start at 0 and increment by 1?
    – MSalters
    Oct 25, 2017 at 12:59
  • @Sean - That doesn't assign both values to weekend. It will not be equal to either. Oct 25, 2017 at 13:14
  • @StoryTeller - I'm aware of that. I'm trying to provide some pointers about what he'd need to change in his example (ie the values of the days)/
    – Sean
    Oct 25, 2017 at 13:16
  • 2
    @Sean - I know you know. But giving a hint like that is only likely to confuse the OP. He doesn't seem to grasp basics here. So my comment is actually geared more towards him. Oct 25, 2017 at 13:19

2 Answers 2

1

It would be very messy to use same type for enumeration and collection of days. You should differentiate between WeekDay as symbols of days and collections of days as in Weekend. Look at int type. Variable int something could contain only one number. If you want more, you have to use collection: array, vector etc.

I assume that you want to create collections of week days where each day appears only once. For this we usually use sets.

This answer contains samples using std::set, then std::bitset, and custom class.

Sample for 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;
}

Unfortunately it isn't looking pretty or elegant. I'm open for improvement.

And std::bitset version:

#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;
}

And custom solution:

#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
  • thank for your answer sincerely;And i will read it conscientiously. Oct 25, 2017 at 17:22
-1

You can declare your enum as power of two. for E.g

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

And now you can assign more than one enum by using | operator between them.

To check if any enum is set use oprator &

1
  • powers of 2 can get typos when they get large. I've always preferred (1<<0), (1<<1), (1<<2), etc. Also, prefer that every name has at least one bit set (Your Saturday has no bits set). Oct 25, 2017 at 18:16

Not the answer you're looking for? Browse other questions tagged or ask your own question.