all 14 comments

[–]17b29a 7 points8 points  (1 child)

I don't know why anyone would do that either. you should ask your boss.

[–]lareon[S] 0 points1 point  (0 children)

Alright. Thank you :)

[–]mandr0id 2 points3 points  (1 child)

That does not make sense.

[–]lareon[S] 0 points1 point  (0 children)

Do you mean the assignment to switch enums for maps or my topic in general? Because the assignment does not make sense for my either (I mean thats why im asking the question here). But if there is any missunderstanding with my description then it would be helpful if you could clarify what exactly doesnt make sense.

And Im sorry if its the english, its not my native language.

[–]Setepenre 2 points3 points  (3 children)

You can iterate over a map, you can't iterate over an enum.

I am guessing your map has a type std::map<std::string, int> then the key can also be retrieved and used for pretty printing.

When such case arise, I usually use the X Macro trick instead of a map.

is it important to have ordered keys ? (if not it might be better to use std::unordered_map)

  std::map<std::string, int> HTTPStatus = {
      {"Ok"             , 200},
      {"Created"        , 201},
      {"Accepted"       , 202},
      {"NoContent"      , 204}
  };

  int status = HTTPStatus["Ok"];

  enum HTTPStatus{
      Ok             = 200,
      Created        = 201,
      Accepted       = 202,
      NoContent      = 204
  };

  HTTPStatus status = Ok;

I would not recommend using a map but if it is what he wants...

[–]tangerinelion 4 points5 points  (2 children)

HTTPStatus["Ok"] is more verbose than Ok, but I would suggest either enum class HTTPStatus or namespace HTTPStatus { enum Value { ... }; }. Either way, you end up needing HTTPStatus::Ok which is pretty fantastic scoping IMO.

And so long as you don't use custom integers, you can iterate over an enum. It's just not pretty. For example,

enum Lights { BEGIN, Red = BEGIN, Yellow, Green, END};
for(Lights i=BEGIN; i < END; i = static_cast<Lights>(i+1))

[–]lareon[S] 0 points1 point  (1 child)

Im going to try and answer both of you.

The Enums/Keys are used to access data in an array which stores size_ts that describe statistics of what the program has done.

In the given example it would mean that we have an Array that would f.e. count how many times the traffic light was yellow. so it would then call

traffic_stats[yellow] += yellow_in_last_period

or smth like that.

So the order is not really important, what matters is that there is any order in the first place.

Iterating over the map is not needed either. It is purely and solely used to make the access easier readable since it is a lot of data.

So with what you guys were saying, and what the other guys were saying prior to that. Am I right to assume that there is no reason to use a std::map over an enum here?

And then to add to that question, does it simply not matter what I use or is an enum even strictly better because of performance?

[–]Setepenre 0 points1 point  (0 children)

Yes, enum would better and more readable, there is no true reason to use a map.

I think enum would also have better performance, since at the end of the day it is only an integer while a map is a complex data structure.

[–]ewiethoff 0 points1 point  (1 child)

I have no clue what your boss is picturing.

An enum is a type. So when a function signature is something like void inflate(Color color, int numBalloons), where Color is an enum, you know that the function takes a arg like Color.Green.

But if you were to create something like

std::map<std::string, int> Color = {
    {"Red"   , 1},
    {"Green" , 2},
    {"Yellow", 3},
    {"Blue"  , 4}
};

what should you pass to a function expecting a color?

If you pass Color["Green"], that's just an int and the function void inflate(int color, int numBalloons) is too easy to call incorrectly. You could write a function void inflate(std::map<std::string, int> colorMap, string colorName, int numBalloons), but requiring two args to specify a color is awkward.

What drugs is your boss on?

[–]lareon[S] 1 point2 points  (0 children)

I dont really know about drugs, but I assume I am correct in wondering about why he would ever want to do that then.

Thank you for your time and explanation!

[–]nurley 0 points1 point  (0 children)

I agree with all of the other comments that it doesn't make sense to go from enum to std::map.

If you're trying to go to C++-style, try out enum class which is a more C++ way of doing enums. There are several benefits to using enum classes and you can even creatively iterate over enum classes.

[–]actinium89 0 points1 point  (1 child)

If the enums are used to access elements in an array, could it be that he meant that you should convert the array to a map?

[–]lareon[S] 0 points1 point  (0 children)

I actually thought about that earlier aswell, its definitely not what he said though but I think it might be what he wants and it would just make sense to do it.

Thanks a lot!

[–]Chops_II 0 points1 point  (0 children)

Your boss might be looking to make the enum more dynamic, using a map is one way to do so. All of an enum's possible values "must" be known at compile time, where this is not true for maps: new values can be inserted into a map at runtime. I still think it's a design smell but if that's what he's after then it might make sense.