您的位置:首页 > 编程语言 > C语言/C++

std::unordered_map用法

2017-02-24 16:12 537 查看

map/unordered_map区别

实现

map为红黑树,内部元素有序

unordered_map内部为哈希表

适用场景

map适合要求元素有序的场景

unordered_map查找较快

初始化

c++11

c++11中简化了操作,可以直接进行初始化

class myClass {
private:
static map<int,int> myMap;
};

//myClass.cpp
map<int,int> myClass::myMap = {
{1, 2},
{3, 4},
{5, 6}
};


非c++11

struct A{
static map<int,int> create_map()
{
map<int,int> m;
m[1] = 2;
m[3] = 4;
m[5] = 6;
return m;
}
static const map<int,int> myMap;

};

const map<int,int> A:: myMap =  A::create_map();


参考:

[1].http://blog.csdn.net/batuwuhanpei/article/details/50727227

[2].http://stackoverflow.com/questions/2636303/how-to-initialize-a-private-static-const-map-in-c
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c++ map