c++中使用hash_map

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/tianwei0822/article/details/82082234

1.需要加上头文件#include<ext/hash_map>

2.需要加上名称空间using namespace __gnu_cxx;

3.当hash_map中使用string或者long long为key时(key为int时不需要),需用户扩展命名空间

代码示例:

#include<bits/stdc++.h>
#include<ext/hash_map>
using namespace std;
using namespace __gnu_cxx;
namespace __gnu_cxx
{
    template<> struct hash< std::string >
    {
        size_t operator()( const std::string& x ) const
        {
            return hash< const char* >()( x.c_str() );
        }
    };

    template<> struct hash<long long>
    {
        size_t operator()(long long x) const
        {
            return x;
        }
    };
};

///hash_map<int,int> ma;
hash_map<string,int> ma;

int main()
{
    ma["ads"]=1;
    cout<<ma["ads"]<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/tianwei0822/article/details/82082234
今日推荐