C++ 如何迭代/计数 multimap<string,string>
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7424805/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to iterate/count for a multimap<string,string>
提问by Anon
My class is like this:
我的课是这样的:
class Outgoing
{
multimap<string,string> outgoing;
public:
void makeConnection(string key, string value)
{
outgoing.insert(pair<string,string>(key,value));
}
void iterate()
{
multimap<string, string>::iterator it;
multimap<string, string>::iterator it2;
pair<multimap<string,string>::iterator,multimap<string,string>::iterator> ret;
for (it = outgoing.begin();it != outgoing.end();++it)
{
ret = outgoing.equal_range((*it)); ??????
for (it2=ret.first; it2!=ret.second; ++it2)
{
???????
}
}
}
};
background:
背景:
I want to represent a graph which can have many nodes. The key won't repeat but can have multiple values.
我想表示一个可以有很多节点的图。键不会重复,但可以有多个值。
str1 ----> val1
str1 ----> val2
str2 -----> val3
I want to know how can I get number of values for a particular key? for e.g. in the above question , for str1 it will be 2?
我想知道如何获取特定键的值数量?例如在上面的问题中,对于 str1 它将是 2?
As you can see , I tried to do something after some digging around but in vain.
正如你所看到的,经过一番挖掘后,我试图做一些事情,但徒劳无功。
What is wrong with my code?
我的代码有什么问题?
thanks
谢谢
EDIT ::: after templatetypedef's comment, I edited the code to:
编辑 ::: 在 templatetypedef 的评论之后,我将代码编辑为:
for (it = outgoing.begin();it != outgoing.end();++it)
{
cout<< (*it).first << " "<< outgoing.count((*it).first);
}
I can get the count, but the key("str1") comes twice. So the answer I see is 2 2 1.
我可以得到计数,但是 key("str1") 出现了两次。所以我看到的答案是 2 2 1。
I would appreciate it very much, if somebody teaches me how to iterate in such a way I get only one key. BTW, thanks, templatetypedef
如果有人教我如何以这样的方式迭代,我将非常感激,我只能得到一个键。顺便说一句,谢谢,templatetypedef
采纳答案by templatetypedef
You can use the count
function for this, which returns the number of entries in the multimap
with the the given key. In your example, writing
您可以count
为此使用该函数,该函数返回multimap
具有给定键的条目数。在你的例子中,写
outgoing.count("str1")
would produce the value 2.
将产生值 2。
In C++, there is no way to iterate over just the unique keys in a multimap
. If you want to iterate over just those keys, there are two options you might want to consider:
在 C++ 中,无法仅迭代multimap
. 如果您只想迭代这些键,您可能需要考虑两个选项:
You could change from using a
multimap< string, string >
tomap<string, vector<string> >
. That way, each key is unique, and you can easily determine how many values are associated with each key by just looking at the number of elements in the correspondingvector
.You could have a top-level loop to iterate over all keys, then have an inner loop to skip duplicate keys.
您可以从使用 a 更改
multimap< string, string >
为map<string, vector<string> >
。这样,每个键都是唯一的,您只需查看相应vector
.您可以有一个顶级循环来迭代所有键,然后有一个内部循环来跳过重复的键。
As an example of option 2, you might try something like this:
作为选项 2 的示例,您可以尝试以下操作:
for (multimap<string, string>::iterator itr = myMap.begin(); itr != myMap.end(); ) {
/* ... process *itr ... */
/* Now, go skip to the first entry with a new key. */
multimap<string, string>::iterator curr = itr;
while (itr != myMap.end() && itr->first == curr->first)
++itr;
}
Hope this helps!
希望这可以帮助!
回答by Marco Fleres
The function equal_range provides a pair of iterators, with the first and last elements of the map thar share a certain key.
函数 equal_range 提供了一对迭代器,映射的第一个和最后一个元素共享某个键。
http://www.cplusplus.com/reference/map/multimap/equal_range/
http://www.cplusplus.com/reference/map/multimap/equal_range/
// multimap::equal_range
#include <iostream>
#include <map>
int main ()
{
std::multimap<char,int> mymm;
mymm.insert(std::pair<char,int>('a',10));
mymm.insert(std::pair<char,int>('b',20));
mymm.insert(std::pair<char,int>('b',30));
mymm.insert(std::pair<char,int>('b',40));
mymm.insert(std::pair<char,int>('c',50));
mymm.insert(std::pair<char,int>('c',60));
mymm.insert(std::pair<char,int>('d',60));
std::cout << "mymm contains:\n";
for (char ch='a'; ch<='d'; ch++)
{
std::pair <std::multimap<char,int>::iterator, std::multimap<char,int>::iterator> ret;
ret = mymm.equal_range(ch);
std::cout << ch << " =>";
for (std::multimap<char,int>::iterator it=ret.first; it!=ret.second; ++it)
std::cout << ' ' << it->second;
std::cout << '\n';
}
return 0;
}