迭代多重映射<string,map =“”<string,int =“”>&gt; </string,>

时间:2013-11-19 04:30:11

标签: c++ iterator multimap

我希望迭代多地图<string,map<string,int>>中的所有项目,但每个密钥只需一次,但我无法使其工作,这是我用来迭代的代码:

for(multimap <string, map <string, int> >::iterator it = myMultimap.begin(); it != myMultimap.end(); it =myMultimap.upper_bound(it->first)){

    //i read that myMultimap.upper_bound(it->first) get the elements of the same key

    pair< multimap <string,map <string, int> >::iterator , multimap <string,map <string, int> >::iterator > ret;
    ret = myMultimap.equal_range(it->first);

    for(multimap <string, map <string, int> >::iterator it2 = ret.first; it2 != ret.second; it2++){


    //here i just want to print map <string , int>
    cout << (*it2).second.first << endl;
    cout << (*it2).second.second << endl;

    }
}

当我运行它时,我得到class std::map<std::basic_string<char>, int>’ don't have a member called ‘first’并且同样的事情为second.second。抱歉,我的英语不是我的母语。

2 个答案:

答案 0 :(得分:1)

让我们自己更轻松地开始

typedef std::map<std::string, int> InnerMap;
typedef std::multimap<std::string, InnerMap> StringMap;

StringMap myMultimap;

现在,那个外循环

for (StringMap::iterator it = myMultimap.begin(); it != myMultimap.end(); ++it)
{
    std::cout << "[" << it->first << "]:";
    for (InnerMap::iterator it2 = it->second.begin(); it2 != it->second.end(); ++it2)
    {
        std::cout << " " << it2->first << ":" << it2->second;
    }
    std::cout << '\n';
}

如果您使用C ++ 11,我们也可以使用auto

简化操作
for (auto it = myMultimap.begin(); it != myMultimap.end(); ++it)
{
    std::cout << "[" << it->first << "]:";
    for (auto it2 = it->second.begin(); it2 != it->second.end(); ++it2)
    {
        std::cout << " " << it2->first << ":" << it2->second;
    }
    std::cout << '\n';
}

我们正在做什么:

for (StringMap::iterator it = myMultimap.begin(); it != myMultimap.end(); ++it)

这会迭代实际构成外部多重映射的所有std::pair<std::string /*key*/, InnerMap /*value*/>元素。如果你有两次密钥,你会看到两个条目。

it->first是当前多地图条目的std::string密钥,it->second是当前条目的InnerMap值。

    for (InnerMap::iterator it2 = it->second.begin(); it2 != it->second.end(); ++it2)

这会迭代地图的std::pair<std::string, int> InnerMap元素,这是此多图插槽的值。

---编辑---

最终,使用C ++ 11你可以使用基于范围(不确定为什么我认为这会更加混乱)

// use 'auto&' so we take a reference instead of a copy.
for (auto& it : myMultimap)
{
    std::cout << "[" << it.first << "]:";
    for (auto it2 : it.second)
    {
        std::cout << " " << it2.first << ":" << it2.second;
    }
    std::cout << '\n';
}

请注意,在这种情况下,我们现在使用“。”而不是“ - &gt;”。这是因为我们实际上在multimap / map中看到了每个元素(std::pair<...>),而不是简单的迭代器。也就是说,it的类型为std::pair<std::string, InnerMap>&,而it2的类型为std::pair<std::string, int>&

答案 1 :(得分:0)

   map<string, int>::iterator in;

 for(multimap <string, map <string, int> >::iterator it = myMultimap.begin(); it != myMultimap.end(); it =myMultimap.upper_bound(it->first)){



        pair< multimap <string,map <string, int> >::iterator , multimap <string,map <string, int> >::iterator > ret;
        ret = myMultimap.equal_range(it->first);

        for(multimap <string, map <string, int> >::iterator it2 = ret.first; it2 != ret.second; it2++){

            //get the map with begin() and assign it to an iterator
            in = it2->second.begin();
            cout << in->first << " "<< in->second << endl;


        }
    }