首页 > 其他 > 详细

我学hash_map(2)

时间:2015-03-06 20:33:27      阅读:304      评论:0      收藏:0      [点我收藏+]

啊,转眼之间就来到了我学hash_map(2)了。我们也从hash_map转移到了unordered_map上来了,今天这个文章的目的就是要来分享一下使用这个hash_map,哦不,unordered_map的一个方法。

直接贴代码……(懒死算了)

#include <iostream>
#include <unordered_map>
using namespace std;

struct node
{
    int a;

    bool operator==(const node & n) const
    {
        return a == n.a;
    }
};

struct Hash
{
    size_t operator()(const node & n) const
    {
        return n.a;
    }
};


int main()
{
    unordered_map<node, int, Hash> ss;
    
    for(int i=0;i<10;i++)
    {
        node tmp;
        tmp.a = i;
        ss[tmp] = i * i;
    }
    for(unordered_map<node,int, Hash>::iterator iter = ss.begin();iter != ss.end();iter++)
    {
        cout << (iter->first).a << "\t" << iter->second << endl;
    }    

}

struct里重载 == 符号,hash函数单独写在外面。

backward_warning.h 这个头文件中已经写出了,使用unordered_map替代ext/hash_map里的hash_map和hash_multimap.

还要注意迭代器的写法,好,结束。

我学hash_map(2)

原文:http://www.cnblogs.com/SuperBlee/p/4319080.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!