C ++使用std :: set<>在std :: map<>

时间:2014-03-11 13:46:59

标签: c++ map iterator set

我想在std :: map

中使用std :: set

我没有使用std :: containers进行过多的exp,所以我不确定我是否正确使用它。 我正在尝试处理一组值,并且在每个集合中都是另一组值。

map<string, set<string> > data_map;

data_map["KEY1"].insert("VAL1");
data_map["KEY1"].insert("VAL2");

data_map["KEY2"].insert("VAL1");
data_map["KEY2"].insert("VAL3");

当我尝试访问map中的set(内部for-cycle)

时,我收到错误
error: no match for call to ‘(std::set<std::basic_string<char> >) ()’|
error: no match for call to ‘(std::set<std::basic_string<char> >) ()’|


for( map<string, set<string> >::iterator mip = data_map.begin();mip != data_map.end(); ++mip) {
    for ( set<string>::iterator sit = mip->second().begin(); sit != mip->second().end(); ++sit )
        cout << *sit << endl;

}

你能告诉我如何迭代所有价值观吗?

3 个答案:

答案 0 :(得分:3)

mip->second().begin()

应该是

mip->second.begin()

答案 1 :(得分:2)

你应该使用mip-&gt; second而不是mip-&gt; second()。 我建议你在for-each循环中使用auto。

for(auto mip : data_map)
{  
    //Do another loop to get the values of your set, to get the set from the map use get<1>(mip);

}

最好以这种方式阅读,减少错误空间。

答案 2 :(得分:0)

不要像使用函数一样调用该集。

for( map<string, set<string> >::iterator mip = lines.begin();mip != lines.end(); ++mip) {
    for ( set<string>::iterator sit = mip->second.begin(); sit != mip->second.end();
          ++sit )
        cout << *sit << endl;
相关问题