遍历 C++ STL 映射数据结构:新技术?(迭代范围和 'auto' 关键字)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14555751/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 18:26:37  来源:igfitidea点击:

Iterating through a C++ STL map data structure: New technique? (Iteration over range and 'auto' keyword)

c++mapstlc++11

提问by KT100

So far I have always used an iterator for traversing through all the keys in an STL map as follows:

到目前为止,我一直使用迭代器来遍历 STL 映射中的所有键,如下所示:

    for (std::map<char,int>::iterator it=mymap.begin(); it!=mymap.end(); ++it){
            std::cout << it->first << "  => " << it->second << '\n';
    }

Very recently though I came across some code that used a different style to iterate through the keys as shown below. Has this feature been added only recently in revised standard? It seems like a rather interesting way of getting more done with lesser code, as many other languages already provide.

最近,虽然我遇到了一些使用不同样式来迭代键的代码,如下所示。是否最近才在修订标准中添加了此功能?正如许多其他语言已经提供的那样,这似乎是一种用更少的代码完成更多工作的有趣方式。

    for (auto& x: mymap) {
            std::cout << x.first << " => " << x.second << '\n';
    }  

Also, I am curious to know the exact implications of using the keyword "auto" here.

另外,我很想知道在此处使用关键字“auto”的确切含义。

回答by Karthik T

This code uses 2 new features from the latest C++ standard (C++11) the auto keyword, for type inference, and the range based for loop.

此代码使用最新 C++ 标准 (C++11) 中的 2 个新功能auto 关键字,用于类型推断基于范围的 for loop

Using just autothis can be written as (thanks Ben)

只使用auto这个可以写成(感谢 Ben)

for (auto it=mymap.begin(); it!=mymap.end(); ++it)

Using just range for this can be written as

仅使用范围可以写成

for (std::pair<const char,int>& x: mymap) {
        std::cout << x.first << " => " << x.second << '\n';
}  

Both of these do the exact same task as your two versions.

这两个版本与您的两个版本执行完全相同的任务。

回答by mcsilvio

The following worked for me:

以下对我有用:

for (auto x: mymap) {
  cout << x.first << endl;
}

回答by BoshWash

In addition to the previous answers, C++17 added another approach using structured bindings:

除了前面的答案之外,C++17 添加了另一种使用结构化绑定的方法:

for (auto& [key, value]: mymap) {
        std::cout << key << " => " << value << '\n';
} 

回答by Alok Save

I am curious to know the exact implications of using the keyword "auto" here.

我很想知道在这里使用关键字“auto”的确切含义。

It enables:

它使:

  • Less typing for a typical iterating code
  • Less chances of manual errors because compiler deduces the exact type of the iterator.
  • 减少典型迭代代码的输入
  • 由于编译器推断出迭代器的确切类型,因此手动错误的机会更少。

回答by billz

It's new feature of C++11, it's called Range-Based for Loops, which iterates over all elements of a given range, array, or collection. It's what in other programming languages would be called a foreach loop The general syntax is as follows:

它是 C++11 的新特性,称为Range-Based for Loops,它迭代给定范围、数组或集合的所有元素。这就是其他编程语言中称为 foreach 循环的一般语法如下:

for ( decl : coll ) {
    statement
}

Auto: Automatic Type Deduction with auto

Auto: 自动类型推导与 auto

With C++11, you can declare a variable or an object without specifying its specific type by using, for example:

使用 C++11,您可以声明一个变量或对象,而无需指定其特定类型,例如:

auto i = 42; // i has type int
double f();
auto d = f(); // d has type double