如何对多重映射进行迭代/计数
我的课程是这样的:
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)
{
???????
}
}
}
};
背景:
我想表示一个可以有很多节点的图。键不会重复,但可以有多个值。
str1 ----> val1
str1 ----> val2
str2 -----> val3
我想知道如何获取特定键的值数量?例如,在上面的问题中,对于 str1 来说,它将是 2?
正如你所看到的,经过一番挖掘后我尝试做一些事情但徒劳无功。
我的代码有什么问题吗?
谢谢
编辑 ::: 在 templatetypedef 的评论之后,我将代码编辑为:
for (it = outgoing.begin();it != outgoing.end();++it)
{
cout<< (*it).first << " "<< outgoing.count((*it).first);
}
我可以得到计数,但是 key("str1") 出现了两次。所以我看到的答案是 2 2 1。
如果有人教我如何以这样的方式进行迭代,我将非常感激,我只得到一个密钥。顺便说一句,谢谢,templatetypedef
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?
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:
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.
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为此,您可以使用
count
函数,该函数返回具有给定键的multimap
中的条目数。在您的示例中,写入将产生值 2。
在 C++ 中,无法仅迭代
multimap
中的唯一键。如果您只想迭代这些键,您可能需要考虑两个选项:您可以更改使用
multimap<;字符串,字符串>
到map<字符串,向量<字符串> >
。这样,每个键都是唯一的,您只需查看相应向量
中的元素数量即可轻松确定与每个键关联的值的数量。您可以使用一个顶级循环来迭代所有键,然后使用一个内部循环来跳过重复的键。
作为选项 2 的示例,您可以尝试如下操作:
希望这会有所帮助!
You can use the
count
function for this, which returns the number of entries in themultimap
with the the given key. In your example, writingwould produce the value 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: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.
As an example of option 2, you might try something like this:
Hope this helps!
函数 equal_range 提供一对迭代器,映射的第一个和最后一个元素共享某个键。
http://www.cplusplus.com/reference/map/multimap/equal_range/
The function equal_range provides a pair of iterators, with the first and last elements of the map thar share a certain key.
http://www.cplusplus.com/reference/map/multimap/equal_range/