如何对多重映射进行迭代/计数

发布于 2024-12-04 18:52:52 字数 1330 浏览 0 评论 0原文

我的课程是这样的:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼泪淡了忧伤 2024-12-11 18:52:52

为此,您可以使用 count 函数,该函数返回具有给定键的 multimap 中的条目数。在您的示例中,写入

outgoing.count("str1")

将产生值 2。

在 C++ 中,无法仅迭代 multimap 中的唯一键。如果您只想迭代这些键,您可能需要考虑两个选项:

  1. 您可以更改使用 multimap<;字符串,字符串>map<字符串,向量<字符串> >。这样,每个键都是唯一的,您只需查看相应向量中的元素数量即可轻松确定与每个键关联的值的数量。

  2. 您可以使用一个顶级循环来迭代所有键,然后使用一个内部循环来跳过重复的键。

作为选项 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;
}

希望这会有所帮助!

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

outgoing.count("str1")

would 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:

  1. You could change from using a multimap< string, string > to map<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 corresponding vector.

  2. 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:

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!

往事随风而去 2024-12-11 18:52:52

函数 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;
}

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/

// 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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文