在命令提示符下在我的g ++(5.1.0)编译器上使用unordered_map显示错误

我最近已将MinGW下载到我的计算机中,但是在使用某些容器和迭代器(例如unordered_map和auto)时会显示意外错误。

我的代码如下:

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


int main()
{

    unordered_map<string,int> umap; 

    umap["GeeksforGeeks"] = 10; 
    umap["Practice"] = 20; 
    umap["Contribute"] = 30; 

    for (auto x : umap) 
      cout << x.first << " " << x.second << endl; 


    return 0;
}

它给出以下错误:


C:\Users\naima\Documents\cpp>g++ -o try2 try2.cpp
In file included from C:/tdm-gcc-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/unordered_map:35:0,from try2.cpp:2:
C:/tdm-gcc-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental,and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
 #error This file requires compiler and library support for the \
  ^
try2.cpp: In function 'int main()':
try2.cpp:9:5: error: 'unordered_map' was not declared in this scope
     unordered_map<string,int> umap;
     ^
try2.cpp:9:25: error: expected primary-expression before ',' token
     unordered_map<string,int> umap;
                         ^
try2.cpp:9:27: error: expected primary-expression before 'int'
     unordered_map<string,int> umap;
                           ^
try2.cpp:11:5: error: 'umap' was not declared in this scope
     umap["GeeksforGeeks"] = 10;
     ^
try2.cpp:15:15: error: 'x' does not name a type
     for (auto x : umap)
               ^
try2.cpp:19:5: error: expected ';' before 'return'
     return 0;
     ^
try2.cpp:19:5: error: expected primary-expression before 'return'
try2.cpp:19:5: error: expected ';' before 'return'
try2.cpp:19:5: error: expected primary-expression before 'return'
try2.cpp:19:5: error: expected ')' before 'return'
asdfghjklqwertyumnb 回答:在命令提示符下在我的g ++(5.1.0)编译器上使用unordered_map显示错误

编译器告诉您确切的问题。通常会。

This file requires compiler and library support for the ISO C++ 2011 standard. This
support is currently experimental,and must be enabled with the -std=c++11 or
-std=gnu++11 compiler options.

您只需要编译适当的标志-std=c++11。我不知道您是否在与版本分级人员使用什么版本匹配,或者是什么版本匹配,但是使用minGW编译器的理由很少,在该编译器中,对8年标准的支持仍然被认为是实验性的。

您可以在这里看到它按预期运行:https://godbolt.org/z/JQxL00 如果您删除-std=c++11标志,它将无法编译并给出相同的错误消息。

您可能还会注意到,我将include更改为仅包含我使用的内容。这样可以大大缩短编译时间,减小可执行文件的大小,并易于理解代码段(因为很容易看到正在使用的标准功能)。您还可以避免污染名称空间。

本文链接:https://www.f2er.com/3150913.html

大家都在问