如何遍历/迭代STL映射?

时间:2010-11-17 17:35:17

标签: c++ dictionary stl traversal

想要遍历STL地图。我不想用它的钥匙。我不关心排序,我只想找到一种方法来访问它包含的所有元素。我怎么能这样做?

6 个答案:

答案 0 :(得分:60)

是的,您可以遍历标准库map。这是用于遍历map的基本方法,并作为遍历任何标准库集合的指导:

C ++ 03 / C ++ 11:

#include <cstdlib>
#include <map>
#include <string>
using namespace std;

int main()
{
    typedef map<int,string> MyMap;
    MyMap my_map;
    // ... magic

    for( MyMap::const_iterator it = my_map.begin(); it != my_map.end(); ++it )
    {
      int key = it->first;
      string value = it->second;
    }
}

如果您需要修改元素:

  • 使用iterator而不是const_iterator
  • 不是将值复制到迭代器之外,而是获取引用并通过它修改值。

    for(MyMap :: iterator it = my_map.begin(); it!= my_map.end(); ++ it) {   int key = it-&gt; first;   串和放大器; value = it-&gt; second;   if(value ==“foo”)     value =“bar”; }

这是您通常手动遍历标准库容器的方式。最大的区别在于,对于map*it的类型是pair而不是元素本身

C ++ 11

如果您受益于C ++ 11编译器(例如,带有--std=c++11或MSVC的最新GCC),那么您还有其他选项。

首先,您可以使用auto关键字来消除所有令人讨厌的冗长:

#include <cstdlib>
#include <map>
#include <string>
using namespace std;

int main()
{
    map<int,string> my_map;
    // ... magic

    for( auto it = my_map.begin(); it != my_map.end(); ++it )
    {
      int key = it->first;
      string& value = it->second;
    }
}

其次,你也可以雇用lambdas。与decltype结合使用,可能会产生更清晰的代码(尽管需要权衡):

#include <cstdlib>
#include <map>
#include <string>
#include <algorithm>
using namespace std;

int main()
{
    map<int,string> my_map;
    // ... magic

    for_each(my_map.begin(), my_map.end(), [](decltype(*my_map.begin()) val)
    {
        string& value = val.second;
        int key = val.first;
    });
}

C ++ 11还引入了基于范围for循环的概念,您可能认为它与其他语言类似。但是,一些编译器还没有完全支持这一点 - 特别是MSVC。

#include <cstdlib>
#include <map>
#include <string>
#include <algorithm>
using namespace std;

int main()
{
    map<int,string> my_map;
    // ... magic

    for(auto val : my_map )
    {
        string& value = val.second;
        int key = val.first;
    }
}

答案 1 :(得分:11)

与任何STL容器一样,begin()end()方法返回可用于迭代地图的迭代器。取消引用地图迭代器会产生std::pair<const Key, Value>

答案 2 :(得分:5)

您可以使用与任何其他STL容器相同的方式遍历STL map:使用迭代器,例如。

for (std::map<key, value>::const_iterator
     i = myMap.begin(), end = myMap.end(); i != end; ++i)
{
    // *i is a key-value pair
}

答案 3 :(得分:1)

C ++ 17

C++17起,您可以将range-based for loopsstructured bindings一起用于遍历地图。产生的代码,例如用于打印地图的所有元素,且简短易读:

std::map<int, std::string> m{ {3, "a"}, {5, "b"}, {9, "c"} };

for (const auto &[k, v] : m)
    std::cout << "m[" << k << "] = " << v << std::endl;

输出:

  

m [3] = a
  m [5] = b
  m [9] = c

Code on Coliru

答案 4 :(得分:0)

您可以使用auto迭代器迭代map。

代码段:

#include<bits/stdc++.h>
using namespace std;

int main()
{
      ios::sync_with_stdio(false);
      map<string, int> mp;

      mp["a"]=500;
      mp["b"]=200;
      mp["d"]=300;
      mp["c"]=400;

      for(auto it=mp.begin(); it != mp.end(); it++)
      {
         cout<<it->first <<" : "<<it->second<<endl;
      }
      return 0;
}

答案 5 :(得分:0)

forauto一起用于C ++ 11及以上版本

map<int,int> map_variable; //you can use any data type for keys, as well as value

for(auto &x:map_variable)
{ 
    cout<<x.first ;// gives the key
    cout<<x.second; //gives the value
}

在C ++ 11中引入了使用for的{​​{1}}的较新格式

要使其具有类似python等高级语言的功能

已经存在这种迭代类型的地方

P.S。 :map变量使值保持排序,因此在迭代时,您将获得按排序顺序排列的键