【问题标题】:Switching on iterator开启迭代器
【发布时间】:2015-06-30 12:52:00
【问题描述】:

我尝试将枚举元素打印为文本而不是数字。代码如下:

#include <iostream>
#include <unordered_set>

enum enm{
    One,
    Two
};

class Complex{
public:
    void func(std::unordered_multiset<int> _v);
    void name(std::unordered_multiset<int>::const_iterator i);
};

void Complex:: name(std::unordered_multiset<int>::const_iterator i){
    switch(*i) {
            case One:
                std::cout<<"One"<<std::endl;
                break;
            case Two:
                std::cout<<"Two"<<std::endl;
                break;
    }
}

void Complex:: func(std::unordered_multiset<int> _v){
    _v.insert(One);
    _v.insert(Two);
    for (std::unordered_multiset<int>::const_iterator i(_v.begin()), end(_v.end()); i != end; ++i){
        std::cout<<"Res: "<<name(i)<<endl;
    }
}

int main(){
    Complex c;
    std::unordered_multiset<int> ms;
    c.func(ms);
    return 0;   
}

我有一个函数name(std::unordered_multiset&lt;int&gt;::const_iterator i),在迭代器上使用开关。问题是 - 它失败了。但我不知道如何解决这个问题以及为什么会发生。

编辑: 我有: 标准::std::cout&lt;&lt;"Res: "&lt;&lt;name(i)&lt;&lt;std::endl; 不仅:name(i);

解决方案 因此,它在以下情况下有效: std::cout<<"Res: "<<std::endl; name(i);

【问题讨论】:

  • “它失败了” 怎么样? Runs fine for me
  • 我明白了——错误:形成对 void 的引用
  • 这不是你昨天问的same question吗?
  • 什么编译器?您可以编辑您的帖子以包含 完整 错误消息
  • 巴里,这是另一个版本,没有超载

标签: c++ enums stl iterator switch-statement


【解决方案1】:

打印语句的顺序将按字典顺序排序,作为urordered map,打印排序后的输出。

【讨论】:

  • 有没有可能的出路?
  • 答案与问题无关,不,不会lexicographically sored , as urordered map
猜你喜欢
  • 2013-06-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-06
  • 1970-01-01
  • 2010-10-27
  • 2018-05-16
相关资源
最近更新 更多