如何遍历/迭代STL映射? [英] How can I traverse/iterate an STL map?

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

问题描述

我想遍历STL映射.我不想使用它的密钥.我不在乎排序,我只是在寻找一种访问其中包含的所有元素的方法.我该怎么办?

I want to traverse an STL map. I don't want to use its key. I don't care about the ordering, I just look for a way to access all elements it contains. How can I do this?

推荐答案

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

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) { 诠释键=它->第一; 弦&值=它->秒; 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"; }

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

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编译器(例如,具有--std=c++11或MSVC的最新GCC),那么您还可以使用其他选项.

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

第二,您也可以使用lambda.结合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映射?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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