-3

I have std::map which its key is an enum like this:

enum class my_enum{a=1,b,c,d};
std::map<my_enum,my_class> my_map;

I want to iterate over the map so I used this for:

for (auto current_type = (id_type)0;
     (int)current_type < 4;
     current_type = (my_enum)((int)current_type + 1)){
   //do things
}

I think it is rubbish. Do you have a better suggestion?

EDIT:

I am totally aware of using of iterators. I know they are prefereable. However, for some reason, I have to stick to the regular index-based for.

Humam Helfawi
  • 19,566
  • 15
  • 85
  • 160
  • Have you consulted some [reference documentation](http://en.cppreference.com/w/cpp/container/map) for iteration facilities offered by `std::map` (`begin()`, `end()` etc.)? Have you tried applying that? What obstacles have you encountered? – Angew is no longer proud of SO Feb 08 '16 at 12:47
  • Of course, I forget to mention that, I am going to edit – Humam Helfawi Feb 08 '16 at 12:47
  • 1
    You can use iterators with a "regular `for`." It's quite unclear what your limitations are. Can you explain in a bit more detail? – Angew is no longer proud of SO Feb 08 '16 at 12:49
  • 1
    Not necessarily the best way, but unscoped `enum` would at least avoid the horrible casts... which, if you must keep them, should be `static_cast(src)` anyway as this is C++ after all. – underscore_d Feb 08 '16 at 12:49
  • @Angew You are right again, but just please suppose I can not. – Humam Helfawi Feb 08 '16 at 12:50
  • 2
    What is a "regular" `for` loop? As far as I am concerned `for (auto it = my_map.begin(), it != my_map.end(), ++it){}` is a "regular" for loop – NathanOliver Feb 08 '16 at 12:50
  • `for (const auto& p : my_map)`, is that a regular for? It isn't clear at all why you need to iterate using the enumeration values. And if you do, then what is the connection to that problem with a map? – juanchopanza Feb 08 '16 at 12:52
  • It is problem in the design. where the enum index(integer value) has its meaning. and I do not want to fix a lot of things which are out of my scope – Humam Helfawi Feb 08 '16 at 12:52
  • This question has been downvoted and has been voted to close because the stupidity of the idea that I am trying to do. However, we all know that in the real life we have to adopt a rubbish code and work from a point where we do not have an option of fixing what was before it. I think downvotes should be about the clearness of the question not because we hate the way that the OP handle his/her problem :) and Many thanks for your time helping me in this :) – Humam Helfawi Feb 08 '16 at 13:11
  • Sorry, but the question it totally unclear, probably because of the irrelevant map thing. How do you know why people down-voted anyway? – juanchopanza Feb 08 '16 at 13:13
  • @juanchopanza maybe you are right. However, I could get a satisfied answer from someone.. – Humam Helfawi Feb 08 '16 at 13:14
  • it is not duplicated. that question handel the case of enums not enums class. and there is only one anser about it and it is exactly as my bad approch – Humam Helfawi Feb 08 '16 at 13:16

2 Answers2

3

To iterate over a map m:

for( auto& item : m ) ...

To iterate over your enum class values:

#include <initializer_list>

enum class my_enum{a=1,b,c,d};

auto main() -> int
{
    using E = my_enum;
    for( my_enum const id : {E::a, E::b, E::c, E::d} ) {}
}

or

enum class my_enum{a=1,b,c,d};

auto main() -> int
{
    for( int i = int( my_enum::a ); i <= int( my_enum::d ); ++i )
    {
        auto const id = my_enum( i );
        // ...
    }
}
Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
1

Define your types like this:

enum class my_enum{a=1, b, c, d, END};
std::map<my_enum,my_class> my_map;

my_enum& operator++( my_enum &val ) {
  using IntType = typename std::underlying_type<my_enum>::type
  val = static_cast<my_enum>( static_cast<IntType>(val) + 1 );
  return val;
}

And then you can use them like this:

for (my_enum e = my_enum::a; e < my_enum::END; ++e) {
   my_class c = my_map[e];
   ....
}

(Although, you might prefer to use something other that operator[] if you don't want to create null-values for entries that don't exist.)

ams
  • 24,923
  • 4
  • 54
  • 75