hash_map C++

来源:互联网 发布:淘宝考试下列旅游景点 编辑:程序博客网 时间:2024/06/18 13:34

参考博客:
http://blog.csdn.net/ddkxddkx/article/details/6555754

编译环境:
win7 、 codeblock (Debugger name and version: GNU gdb (GDB) 7.6.1)

/*    hash_map    关于头文件:        hash_map不是标准的头文件,在c++11标准下,标准头文件是<unordered_map>        ps.            <hash_map> 不是标准头文件. 在 C++11 的标准下, 标准头文件是 <unordered_map>.            如果你的 gcc 有支持 C++11 标准, 建议你用這個. 如果你的 gcc 沒有, 可以考虑用 boost 的函数库.            不要用 using namespace std. 因为会造成名字冲突. 尽量养成 std::xxx 的习惯.        In Visual C++:(未测试)            .NET 2003, members of the <hash_map> and <hash_set> header files are no longer in the std namespace, but rather have been moved into the stdext namespace. See The stdext Namespace for more information.            也就是VC下要用stdext::hash_map        GNU-GCC下:            __gnu_cxx::hash_map    prototype:        template <class _Key,            class _Tp,            class _HashFcn = hash<_Key>,            class _EqualKey = equal_to<_Key>,            class _Alloc = __STL_DEFAULT_ALLOCATOR(_Tp)        >        class hash_map        {                ...        }    对于自己的类型,需要指定hash的计算方式,以及相等函数(查找用)*/#include<iostream>#include<hash_map>#include<map>#include<string>//#include<pair>    //X#include<utility>   //pair#include<stdlib.h>  //system("pause");using namespace std;/*    自定义类型*/class CA{public:    CA(int a):m_ia(a)    {    }//    friend operator<<(ostream &out, CA ca)   //不正确的重载 (我大概自己就没写过重载输出。。。//    {//        out << ca.m_ia;//    }/*  注意:1. 要将输出流返回; 2. 友元函数 3. 输入参数为const类型!!!(不然会报一个什么错误。。。)*/    friend ostream & operator<<(ostream & output, const CA & a)    {        output << a.m_ia;        return output;    }    int get()const    {        return m_ia;    }    bool operator==(const CA & ca)const    {        return this->m_ia == ca.m_ia;    }private:    int m_ia;};//自定义类型对应的hash函数size_t hash_ca(CA &ca){    return ca.get();}//以仿函数的形式定义 - 重载括号//运算符重载函数没有写成const函数,会有以下错误//error: passing 'const hasher {aka const ca_hash}' as 'this' argument of 'size_t ca_hash::operator()(const CA&)' discards qualifiers [-fpermissive]|struct ca_hash{    size_t operator()(const CA &ca)const    {        return (size_t)ca.get();    }};//提供比较函数 - 仿函数struct ca_equal{    bool operator()(const CA & ca, const CA &cb)const    {        return ca.get() == cb.get();    }};typedef __gnu_cxx::hash_map<int, string> hash_map_int;typedef __gnu_cxx::hash_map<string, string> hash_map_str;typedef __gnu_cxx::hash_map<CA, string, ca_hash> hash_map_CA;   //CA自身重载了==,就不用传入这个参数//typedef __gnu_cxx::hash_map<CA, string, ca_hash, ca_equal> hash_map_CA;//函数模板 - 打印hash_map中的信息template <class T>map_print(T m){    typename T::iterator iter;    for(iter = m.begin(); iter != m.end(); ++iter)    {        cout << '(' << iter->first << ", " << iter->second << ')' << endl;    }}int main(){    /*        当key为普通数据类型(整型 包括指针但不包括long long ?)时        不需要提供自己的hash和等于比较函数    */    hash_map_int hmi;    //几种赋值方式    hmi[610] = "cout";    hmi.insert({10, "hello"});    hmi.insert( make_pair(12, "world"));    vector < pair<int, string> > vec_i_str;    vec_i_str.push_back (make_pair(99, "lemmon"));    vec_i_str.push_back(make_pair(120, "tree"));    vec_i_str.push_back(make_pair(77, "yellow"));    hmi.insert(vec_i_str.begin(), vec_i_str.end());     //用vector来初始化    //查找    hash_map_int::iterator iter_1;    iter_1 = hmi.find(10);    if(iter_1 != hmi.end())        cout << iter_1->first << ' ' << iter_1->second << endl;    if(hmi.find(12) != hmi.end())    {        cout << hmi[12] << endl;    }    cout << endl;    //遍历    map_print(hmi);    cout << endl;    //删除    hmi.erase(10);    cout << "erase 10:" << endl;    map_print(hmi);    cout << endl;    /*        自定义类型作为key    */    cout << endl << "Self-defined class as the key of hash_map" << endl;    hash_map_CA hmca;    hmca[10] = "it";    hmca.insert({67, "is"});    hmca.insert(make_pair(99, "just"));    map_print(hmca);    system("pause");    return 0;}
原创粉丝点击