I would like to see a hash_map example in C++

前端 未结 5 800
广开言路
广开言路 2020-12-08 03:41

I don\'t know how to use the hash function in C++, but I know that we can use hash_map. Does g++ support that by simply including #include

5条回答
  •  旧巷少年郎
    2020-12-08 04:41

    The current C++ standard does not have hash maps, but the coming C++0x standard does, and these are already supported by g++ in the shape of "unordered maps":

    #include 
    #include 
    #include 
    using namespace std;
    
    int main() {
        unordered_map  m;
        m["foo"] = 42;
        cout << m["foo"] << endl;
    }
    

    In order to get this compile, you need to tell g++ that you are using C++0x:

    g++ -std=c++0x main.cpp
    

    These maps work pretty much as std::map does, except that instead of providing a custom operator<() for your own types, you need to provide a custom hash function - suitable functions are provided for types like integers and strings.

提交回复
热议问题