遍历无序映射 C++

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22880431/
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-28 00:15:14  来源:igfitidea点击:

Iterate through unordered map C++

c++iteratorunordered-map

提问by Yoda

I have wrote program which reads input until you hit ',' - COMA at the input. Then it counts the number of letters you put in,

我已经编写了读取输入的程序,直到您在输入处点击 ',' - COMA。然后它会计算你输入的字母数,

I want to iterate through this map but it says that itcannot be defined wih no type:

我想遍历这个地图,但它说it不能在没有类型的情况下定义:

#include <iostream>
#include <conio.h>
#include <ctype.h>

#include <iostream>
#include <string>
#include <tr1/unordered_map>
using namespace std;

int main(){
    cout<<"Type '.' when finished typing keys: "<<endl;
    char ch;
    int n = 128;
    std::tr1::unordered_map <char, int> map;


    do{
      ch = _getch();
    cout<<ch;
      if(ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z'){
            map[ch] = map[ch] + 1;
      }
    } while( ch != '.' );

    cout<<endl;

    for ( auto it = map.begin(); it != map.end(); ++it ) //ERROR HERE
        std::cout << " " << it->first << ":" << it->second;


    return 0;
}

回答by Basile Starynkevitch

You are using autoso you have C++11code. You need a C++11 compliant compiler (e.g. GCC 4.8.2 or newer). As Peter G.commented, don't name your variable map(which is std::map) but e.g. mymapSo please

您正在使用,auto所以您拥有C++11代码。您需要一个符合 C++11 的编译器(例如 GCC 4.8.2 或更新版本)。正如Peter G.评论的那样,不要命名您的变量map(即std::map),而是例如mymap所以请

#include <unordered_map>

(no need for tr1!)

(不需要tr1!)

Then compile with g++ -std=c++11 -Wall -g yoursource.cc -o yourprogand code a range based for loop

然后使用基于范围的 for 循环进行编译g++ -std=c++11 -Wall -g yoursource.cc -o yourprog和编码

for (auto it : mymap) 
    std::cout << " " << it.first << ":" << it.second << std::endl;

回答by Dorin Laz?r

With C++17 you can use a shorter, smarter version, like in the code below:

使用 C++17,您可以使用更短、更智能的版本,如下面的代码所示:

unordered_map<string, string> map;
map["hello"] = "world";
map["black"] = "mesa";
map["umbrella"] = "corporation";
for (const auto & [ key, value ] : map) {
    cout << key << ": " << value << endl;
}

回答by Walter

Add -std=c++11to your compiler flags (with gcc/icc/clang) if you want to use auto(and other C++11 features). Btw, unordered_mapis in stdin C++11 ... Also there is std::isalpha...

-std=c++11如果您想使用auto(和其他 C++11 功能),请添加到您的编译器标志(使用 gcc/icc/clang )。顺便说一句,unordered_mapstd在C ++ 11 ...也有std::isalpha...

回答by Erik Campobadal

Based on Dorin Laz?r answer, another possible solution is:

基于 Dorin Laz?r 的回答,另一种可能的解决方案是:

unordered_map<string, string> my_map;
my_map["asd"] = "123";
my_map["asdasd"] = "123123";
my_map["aaa"] = "bbb";
for (const auto &element : my_map) {
    cout << element.first << ": " << element.second << endl;
}