【问题标题】:How can I iterate over a map with a pair as key?如何以一对作为键迭代地图?
【发布时间】:2015-02-01 13:41:04
【问题描述】:

我有一个map,其中pair<int, int> 作为键,第三个整数作为值。如何遍历地图的键以打印它们?我的示例代码粘贴在下面:

#include <iostream>
#include <map>
#include <algorithm>

using namespace std;

int main ()
{
  map <pair<int, int>, int> my_map;
  pair <int, int> my_pair;

  my_pair = make_pair(1, 2);
  my_map[my_pair] = 3;

  // My attempt on iteration
  for(map<pair<int,int>,int>::iterator it = my_map.begin(); it != my_map.end(); ++it) {
    cout << it->first << "\n";
  }

  return 0;
}

我必须如何修改cout 行,才能正常工作?

【问题讨论】:

  • 你想做什么?
  • 您的问题是什么? (顺便说一句:ostream&amp;, pair&lt;X,Y&gt; 没有过载。)在打印 it-&gt;first.first 时有效:coliru.stacked-crooked.com/a/686ef9dc8e2f05c0
  • @VladfromMoscow:我想遍历键并打印它们。
  • 顺便说一句,range-for 会更好:for(auto&amp;&amp; x : my_map) cout &lt;&lt; x.first.first &lt;&lt; ' ' &lt;&lt; x.first.second &lt;&lt; '\n';

标签: c++ dictionary iterator output stdmap


【解决方案1】:
#include <iostream>
#include <map>
#include <algorithm>

using namespace std;

int main()
{
    map <pair<int, int>, int> my_map;
    pair <int, int> my_pair;

    my_pair = make_pair(1, 2);
    my_map[my_pair] = 3;

    // My attempt on iteration
    for (map<pair<int, int>, int>::iterator it = my_map.begin(); it != my_map.end(); ++it) {
        cout << it->first.first << "\n";
        cout << it->first.second << "\n";
    }

    return 0;
}

【讨论】:

    【解决方案2】:

    你是否可以访问c++11,auto关键字可以让它看起来更好看。我编译并运行得很好。

    #include <iostream>
    #include <map>
    #include <algorithm>
    
        using namespace std;
    
        int main ()
        {
          map <pair<int, int>, int> my_map;
          pair <int, int> my_pair;
    
          my_pair = make_pair(1, 2);
          my_map[my_pair] = 3;
    
          // My attempt on iteration
          for(auto it = my_map.begin(); it != my_map.end(); ++it) {
            cout << it->first.first << "\n";
          }
    
          return 0;
        }
    

    【讨论】:

    • 基于for 的范围可以让它看起来更好:)
    【解决方案3】:

    好吧,map Key 是第一项,但它本身是一对

    所以

    pair<int,int>& key(*it);
    cout << key.first << " " << key.second << "\n";
    

    可能会成功

    【讨论】:

      【解决方案4】:

      it-&gt;firstconst std::pair&lt;int, int&gt; 类型的对象,它是键。 it-&gt;secondint 类型的对象,即它是映射值。如果您只想输出键和映射值,您可以编写

      for ( map<pair<int,int>,int>::iterator it = my_map.begin(); it != my_map.end(); ++it ) 
      {
          cout << "( " << it->first.first << ", " 
                       << it->first.second 
                       << " ): "
                       << it->second
                       << std::endl;
      }
      

      或者你可以使用基于范围的 for 语句

      for ( const auto &p : my_map )
      {
          cout << "( " << p.first.first << ", " 
                       << p.first.second 
                       << " ): "
                       << p.second
                       << std::endl;
      }
      

      【讨论】:

        【解决方案5】:

        默认情况下,std::ostream 不知道如何处理std::pair。因此,如果您想打印您的密钥,最简单的方法是直接访问它们的两个元素,如其他答案所示。但是,如果您可以访问C++11 功能,那么您也可以使用range-based for loop 来迭代您的地图,如下所示:

        int main() {
            std::map<std::pair<int, int>, int> my_map{ {{1, 2}, 3}, {{4, 5}, 6}, {{7, 8}, 9} };
        
            for (auto const &kv : my_map)
                std::cout << "(" << kv.first.first << ", " << kv.first.second << ") = " << kv.second << std::endl;
        
            return 0;
        }
        

        输出:

        (1, 2) = 3
        (4, 5) = 6
        (7, 8) = 9

        如果你可以使用C++17,那么你可以进一步缩短代码使用structured binding 在基于范围的for 循环中如下:

        for (auto const &[k, v] : my_map)
            std::cout << "(" << k.first << ", " << k.second << ") = " << v << std::endl;
        

        Code on Coliru

        【讨论】:

          猜你喜欢
          • 2018-03-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-12-23
          相关资源
          最近更新 更多