繁体   English   中英

遍历多图 <string, map <string,int> &gt;

[英]iterate over multimap <string, map <string,int> >

我想遍历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 class std::map<std::basic_string<char>, int>' don't have a member called 'first'相同的东西。 对不起,我的英语不是我的母语。

让我们更轻松地开始

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键, InnerMap 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';
}

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

   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;


        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM