【问题标题】:iterate through a "map" where the value is "enum"遍历值为“枚举”的“地图”
【发布时间】:2014-06-14 17:52:08
【问题描述】:

我有一张地图:map<string, Operations> 其中 Operations 是一个枚举

enum class Operations 
{
    div,
    mul,
    add,
    sub
 };

我是这样填写地图的:

myMap.insert( make_pair("add", Operations::add) );
myMap.insert( make_pair("sub", Operations::sub) );
myMap.insert( make_pair("div", Operations::div) );
myMap.insert( make_pair("mul", Operations::mul) );

如何正确遍历此地图?特别是要获得第二个元素?,(值)。 我收到编译错误。

代码如下:

#include <iostream>
#include <map>

using namespace std;

enum class Operations
{
    add,
    sub,
    div,
    mul
};

template <class K, class V>
void displayMap ( map<K,V> &m )
{
    typename map<K,V>::iterator inx = m.begin();
    for ( ;inx != m.end(); ++inx )
    {
        cout << inx-> first << " " << inx->second << endl;
    }
}


int main ( )
{

    static map<string, Operations> myMap;

    myMap.insert( make_pair ("add", Operations::add) );
    myMap.insert( make_pair ("sub", Operations::sub) );
    myMap.insert( make_pair ("div", Operations::div) );
    myMap.insert( make_pair ("mul", Operations::mul) );

    displayMap (myMap);

    return 0;
}

这是错误信息:

error: invalid operands to binary expression ('basic_ostream<char,
  std::__1::char_traits<char> >' and 'Operations')
        cout << inx-> first << " " << inx->second << endl;

提前致谢! 狮子座

【问题讨论】:

  • 请显示您的编译器和编译命令
  • 当您将Operations 值插入cout 时,您希望打印什么?
  • i686-apple-darwin11-llvm-g++-4.2 (GCC) clang++ -std=c++11 -stdlib=libc++ -g -o fillMapEnum -Wall -Wextra fillMapEnum.cpp
  • myMap 没有理由成为static
  • 这个问题与地图或迭代无关。先解决打印枚举的问题。

标签: c++ c++11 map stl enums


【解决方案1】:

您正在使用不允许隐式转换到基础类型的范围枚举。这就是 C++11 添加它们的重点。您必须将它们显式转换为基础类型。

#include <type_traits>

cout << inx-> first 
     << " " 
     << static_cast<typename std::underlying_type<V>::type>(inx->second) 
     << endl;

Live demo


另一方面,如果您想显示除枚举器值以外的其他内容,您可以创建一个 operator&lt;&lt; 重载来打印您想要的任何内容。

std::ostream& operator<<(ostream& os, Operations op)
{
    switch(op)
    {
        case Operations::add:
            return os << '+';
        case Operations::sub:
            return os << '-';
        case Operations::div:
            return os << '/';
        case Operations::mul:
            return os << '*';
        default: throw std::runtime_error("illegal Operation");
    }
}

Live demo

【讨论】:

    【解决方案2】:

    问题在于std::ostream 无法打印Operations,因为没有operator&lt;&lt; 重载(并且Operations 不能隐式转换为其基础类型)

    你可以如下实现:

    std::ostream& operator<<(std::ostream& os, Operations op)
    {
        switch (op)
        {
            case Operations::div: return os << "div";
            case Operations::mul: return os << "mul";
            case Operations::add: return os << "add";
            case Operations::sub: return os << "sub";
            // (omit default case to enable compiler warning for missing case)
        }
    }
    

    (另请注意,此重载必须在定义 Operator 的同一命名空间中定义。)

    【讨论】:

      猜你喜欢
      • 2010-12-12
      • 1970-01-01
      • 2011-09-01
      • 1970-01-01
      • 2015-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多