如何遍历/迭代STL Map? [英] How to traverse/iterate STL Map?

查看:421
本文介绍了如何遍历/迭代STL Map?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要知道如何遍历stl地图。我不想使用它的键。我不在乎订单,只是一种访问它包含的所有元素的方法。有没有办法这样做?

In need to know how to traverse an stl map. I don't want to use its key. I don't care about the ordering, just a way to access all elements it contains. Is there a way to do this?

推荐答案

是的,你可以遍历标准库 code>。这是用于遍历地图的基本方法,并用作遍历任何标准库集合的指导:

Yes, you can traverse a Standard Library map. This is the basic method used to traverse a map, and serves as guidance to traverse any Standard Library collection:

#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

  • 而不是将值从复制器中复制出来,获取引用并修改这些值。

  • Use iterator rather than const_iterator.
  • Instead of copying the values out of the iterator, get a reference and modify the values through that.

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

for( MyMap::iterator it = my_map.begin(); it != my_map.end(); ++it ) { int key = it->first; string& value = it->second; if( value == "foo" ) value = "bar"; }

这是您通常手动移动标准库容器的方式。最大的区别是,对于地图 *的类型,它是一个 code>而不是元素本身

This is how you typically traverse Standard Library containers by hand. The big difference is that for a map the type of *it is a pair rather than the element itself

如果您有好处一个C ++ 11编译器(例如,最新的GCC与 - std = c ++ 11 或MSVC),那么你也有其他选项。

If you have the benefit of a C++11 compiler (for example, latest GCC with --std=c++11 or MSVC), then you have other options as well.

首先,您可以使用 auto 关键字来摆脱所有这些烦恼:

First you can make use of the auto keyword to get rid of all that nasty verbosity:

#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 ,这可能会导致更清晰的代码(尽管有折衷):

Second, you can also employ lambdas. In conjunction with decltype, this might result in cleaner code (though with tradeoffs):

#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。

C++11 also instroduces the concept of a range-bases for loop, which you may recognize as similar to other languages. However, some compilers do not fully support this yet -- notably, 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;
    }
}

这篇关于如何遍历/迭代STL Map?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆