C++ error: 'unordered_map' does not name a type

前端 未结 3 895
感动是毒
感动是毒 2021-01-08 00:41

I am doing everything correctly as far as I can tell and I have gotten the error message:

error: \'unordered_map\' does not name a type
error: \'mymap\' does         


        
相关标签:
3条回答
  • 2021-01-08 00:48

    Compile with g++ -std=c++11 (my gcc version is gcc 4.7.2) AND

    #include <unordered_map>
    #include <string>
    
    using namespace std;
    
    //global variable
    unordered_map<string,int> mymap;
    
    int main() {
      mymap.reserve(7000); // <-- try putting it here
      return 0;
    }
    
    0 讨论(0)
  • 2021-01-08 01:05

    You can't execute arbitrary expressions at global scope, so you should put

    mymap.reserve(7000);
    

    inside main.

    This is also true for other STL containers like map and vector.

    0 讨论(0)
  • 2021-01-08 01:08

    If you want to support <unordered_map> for versions older than c++11 use
    #include<tr1/unordered_map> and declare your maps in the form :- std::tr1::unordered_map<type1, type2> mymap
    which will use the technical report 1 extension for backward compatibility.

    0 讨论(0)
提交回复
热议问题