Search code examples
c++cmdg++unordered-mapauto

Using unordered_map on my g++ (5.1.0) compiler in command prompt shows error


I have recently downloaded MinGW into my computer but on using certain containers and iterators like unordered_map and auto it shows an unexpected error.

my code is as follows :

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

it gives the following error :


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'

Solution

  • The compiler told you exactly what was wrong. It usually will.

    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.
    

    You just have to compile with the proper flag, -std=c++11. I don't know if you are version-matching against what graders use or what, but there are very few good reasons to be on a minGW compiler where support for an 8 year old standard is still considered experimental.

    You can see that it works as expected here: https://godbolt.org/z/JQxL00 If you remove the -std=c++11 flag, it will fail to compile and give you the same error message.

    You might also notice that I altered the includes to only include what I use. This results in a much faster compile time, smaller executable, and an easier to understand piece of code (Since it is plain to see what standard features are being used). You also avoid polluting your namespace.