String to enum in C++

后端 未结 10 1647
渐次进展
渐次进展 2020-11-28 07:15

Is there a way to associate a string from a text file with an enum value?

The problem is: I have a few enum values stored as string in a text file which I read on

10条回答
  •  被撕碎了的回忆
    2020-11-28 07:55

    You can calculate the hash of the string and then use this:

    template 
    E map_hash(H const key, std::initializer_list> const il)
    {
      auto const i(
        std::find_if(il.begin(),
          il.end(),
          [key](auto& p)
          {
            return p.first == key;
          }
        )
      );
    
      assert(i != il.end());
    
      return i->second;
    }
    

提交回复
热议问题