参考:http://www.cplusplus.com/reference/unordered_map/unordered_map/?kw=unordered_mapios
template < class Key, // unordered_map::key_type class T, // unordered_map::mapped_type class Hash = hash<Key>, // unordered_map::hasher class Pred = equal_to<Key>, // unordered_map::key_equal class Alloc = allocator< pair<const Key,T> > // unordered_map::allocator_type > class unordered_map;
无序的映射
无序映射是存储键值和映射值组合造成的元素的关联容器,它容许根据键快速检索单个元素。
在unordered_map中,键值一般用于唯一地标识元素,而映射的值是一个对象,其内容与此键相关联。键和映射值的类型可能不一样。
在内部,unordered_map中的元素没有对键值或映射值以任何特定的顺序排序,但组织成buckets的形式都取决于他们的散列值,以便经过它的键值快速访问单个元素(平均一个恒定的平均时间复杂度)。
unordered_map容器比map容器能更快地经过它们的键访问单个元素,尽管它们一般对于元素子集的范围迭代效率较低。
无序映射实现了直接访问操做符(operator[]),该操做符容许使用其键值做为参数直接访问映射值。
容器中的迭代器至少是前向迭代器forward iterators。算法
Container properties容器属性数组
无序容器使用散列表来组织它们的元素,散列表容许经过它们的键快速访问元素。app
每一个元素都将一个键关联到一个映射值:键表示标识其主要内容为映射值的元素。dom
容器使用一个分配器对象来动态地处理它的存储需求。函数
key:性能
键值的类型。unordered_map中的每一个元素都由其键值惟一标识。
别名为成员类型unordered_map::key_type。测试
T:优化
映射值的类型。unordered_map中的每一个元素都用于将一些数据存储为其映射值。
别名为成员类型unordered_map::mapped_type。注意,这与unordered_map::value_type(参见下面)不一样。ui
typedef pair<const Key, T> value_type;
unordered_map容器的迭代器指向此value_type的元素。所以,对于一个指向map元素的迭代器来讲,它的键和映射值能够分别经过如下方式访问:
unordered_map<Key,T>::iterator it; (*it).first; // the key value (of type Key) (*it).second; // the mapped value (of type T) (*it); // the "element value" (of type pair<const Key,T>)
固然,任何其余直接访问操做符,如->或[]均可以使用,例如:
it->first; // same as (*it).first (the key value) it->second; // same as (*it).second (the mapped value)
如下别名是unordered_map的成员类型。它们被成员函数普遍用做参数和返回类型:
成员类型 | 定义 | notes |
key_type | 第一个模版参数(Key) | |
mapped_type | 第二个模版参数(T) | |
value_type | pair<const key_type,mapped_type> | |
hasher | 第三个模版参数(Hash) | 默认为:hash<key_type> |
key_equal | 第四个模版参数(Pred) | 默认为:equal_to<key_type> |
allocator_type | 第五个模版参数(Alloc) | 默认为:allocator<value_type> |
reference | Alloc::reference | |
const_reference | Alloc::const_reference | |
pointer | Alloc::pointer | 默认为allocator: value_type* |
const_pointer | Alloc::const_pointer | 默认为allocator: const value_type* |
iterator | value_type的前向迭代器 | |
const_iterator | const value_type的前向迭代器 | |
local_iterator | value_type的前向迭代器 | |
const_local_iterator | const value_type的前向迭代器 | |
size_type | 一个无符号的整数类型 | 等同于size_t |
difference_type | 一个有符号的整数类型 | 等同于ptrdiff_t |
1)构造函数
empty (1) explicit unordered_map ( size_type n = /* see below */, const hasher& hf = hasher(), const key_equal& eql = key_equal(), const allocator_type& alloc = allocator_type() ); explicit unordered_map ( const allocator_type& alloc );
range (2) template <class InputIterator> unordered_map ( InputIterator first, InputIterator last, size_type n = /* see below */, const hasher& hf = hasher(), const key_equal& eql = key_equal(), const allocator_type& alloc = allocator_type() );
copy (3) unordered_map ( const unordered_map& ump ); unordered_map ( const unordered_map& ump, const allocator_type& alloc );
move (4) unordered_map ( unordered_map&& ump ); unordered_map ( unordered_map&& ump, const allocator_type& alloc );
initializer list (5) unordered_map ( initializer_list<value_type> il, size_type n = /* see below */, const hasher& hf = hasher(), const key_equal& eql = key_equal(), const allocator_type& alloc = allocator_type() );
构建unordered_map
构造一个unordered_map容器对象,根据使用的构造函数版本初始化其内容:
(1)空容器构造函数(默认构造函数)
构造一个空的unordered_map对象,该对象不包含元素,大小为0。
它可使用特定的hasher、key_equal和分配器对象以及最少数量的散列桶来构造容器。
(2)range构造函数
构造一个unordered_map对象,该对象包含[first,last)范围内每一个元素的副本。
(3)复制构造函数(以及复制分配器)
对象初始化为具备与unordered_map对象ump相同的内容和属性。
(4)移动构造函数(以及使用分配器移动)
对象获取rvalue右值ump的内容。
(5)初始化器列表
用列表的内容初始化容器。
参数:
n:
初始桶的最小数量。
这不是容器中的元素数量,而是构造内部哈希表所需的最小槽数。
若是没有指定这个参数,构造函数将自动肯定(以一种依赖于特定库实现的方式)。
成员类型size_type是无符号整数类型。
hf:
hasher函数对象。hasher是一个基于做为参数传递给它的容器对象键值key而返回整数值的函数,即该整数值 = hasher(key)。
成员类型hasher在unordered_map中定义为其第三个模板参数(Hash)的别名。
eql:
若是做为参数传递的两个容器对象键key被认为是相等的,则返回true。
成员类型key_equal在unordered_map中定义为其第四个模板参数(Pred)的别名。
alloc:
要使用的分配器对象,而不是构造新对象。
对于使用默认分配器类模板版本的类实例化,此参数不相关。
成员类型allocator_type在unordered_map中定义为其第五个模板参数(Alloc)的别名。
#include <iostream> #include <string> #include <unordered_map> using namespace std; using stringmap = unordered_map<string, string>; stringmap merge(stringmap a, stringmap b){ stringmap temp{a}; temp.insert(b.begin(), b.end()); return temp; } int main(){ stringmap first; stringmap second {{"apple", "red"},{"lemon","yellow"}}; stringmap third {{"orange","orange"}, {"strawberry", "red"}}; stringmap fourth (second); stringmap fifth (merge(third, fourth)); stringmap sixth (fifth.begin(), fifth.end()); cout << "sixth contains : "; for(auto& x:sixth) cout << " " << x.first << ":" << x.second; cout << endl; return 0; }
返回:
sixth contains : orange:orange strawberry:red lemon:yellow apple:red
2)析构函数
~unordered_map();
销毁无序的映射
销毁容器对象。这将调用所包含元素的每一个析构函数,并释放由unordered_map容器分配的全部存储容量。
3)赋值操做
copy (1) unordered_map& operator= ( const unordered_map& ump );
move (2) unordered_map& operator= ( unordered_map&& ump );
initializer list (3) unordered_map& operator= ( intitializer_list<value_type> il );
赋值内容
赋值ump(或il)做为容器的新内容。
在调用以前包含在对象中的元素被销毁,并被unordered_map ump或初始化器列表il中的元素替换(若是有的话)。
第一个版本(1)执行一个复制赋值,它将ump的全部元素复制到容器对象中(ump保留其内容)。
第二个版本(2)执行移动分配,将ump内容的全部权转移给对象。没有赋值发生:内容被ump丢弃。
第三个版本(3)将初始化器列表il的内容赋值为容器对象的元素。
参数:
#include <iostream> #include <string> #include <unordered_map> using namespace std; using stringmap = unordered_map<string, string>; stringmap merge(stringmap a, stringmap b){ stringmap temp{a}; temp.insert(b.begin(), b.end()); return temp; } int main(){ stringmap first, second, third; first = {{"AAPL","Apple"},{"MSFT","Microsoft"}}; //初始化列表 second = {{"GOOG", "Google"}, {"ORCL", "Oracle"}}; third = merge(first, second); first = third; cout << "first contains : "; for(auto& x:first) cout << " " << x.first << ":" << x.second; cout << endl; return 0; }
返回:
first contains : GOOG:Google ORCL:Oracle MSFT:Microsoft AAPL:Apple
4)empty
bool empty() const noexcept;
测试容器是否为空
返回一个bool值,该值指示unordered_map容器是否为空,即其大小是否为0。
此函数不以任何方式修改容器的内容。要清除数组对象的内容,成员函数unordered_map::clear是存在的。
参数:
none
返回值:
若是容器大小为0则返回true,不然返回false
举例:
#include <iostream> #include <unordered_map> using namespace std; int main(){ unordered_map<int, int> first; unordered_map<int, int> second = {{1,10},{2,20},{3,30}}; cout << "first " << (first.empty()?"is empty":"is not empty") << endl; cout << "second " << (second.empty()? "is empty" : "is not empty") << endl; return 0; }
返回:
first is empty second is not empty
5)size
size_type size() const noexcept;
返回容器大小
返回unordered_map容器中的元素数量。
参数:
none
返回值:
容器中元素的数量。
成员类型size_type是无符号整数类型。
举例:
#include <iostream> #include <string> #include <unordered_map> using namespace std; int main(){ unordered_map<string,double> mymap ={ {"milk", 2.30}, {"potatoes", 1.90}, {"eggs", 0.40} }; cout << "mymap.size() is " << mymap.size() << endl; return 0; }
返回:
mymap.size() is 3
6)max_size
size_type max_size() const noexcept;
返回最大大小
返回unordered_map容器能够容纳的元素的最大数量。
这是因为系统约束或其库实现的限制,容器所能容纳的最大潜在元素数。
参数:
none
返回值:
对象能够做为内容容纳的元素的最大数目。
成员类型size_type是无符号整数类型。
举例:
#include <iostream> #include <unordered_map> using namespace std; int main(){ unordered_map<int,int> mymap; cout << "max_size = " << mymap.max_size() << endl; cout << "max_bucket_count = " << mymap.max_bucket_count() << endl; cout << "max_load_factor = " << mymap.max_load_factor() << endl; return 0; }
返回:
max_size = 768614336404564650 max_bucket_count = 768614336404564650 max_load_factor = 1
7)begin
container iterator (1) iterator begin() noexcept; const_iterator begin() const noexcept;
bucket iterator (2) local_iterator begin ( size_type n ); const_local_iterator begin ( size_type n ) const;
返回迭代器开始
返回一个迭代器,该迭代器指向unordered_map容器(1)或其中一个桶(2)中的第一个元素。
注意,unordered_map对象不能保证哪一个特定的元素被认为是它的第一个元素。可是,在任何状况下,从开始到结束的范围覆盖容器(或桶)中的全部元素,直到失效。
参数:
n:
桶数。这应该小于bucket_count。
它是一个可选参数,能够改变这个成员函数的行为:若是设置,迭代器将检索到该桶的第一个元素,不然它将指向容器的第一个元素。
成员类型size_type是无符号整数类型。
返回值:
容器(1)或桶(2)中的第一个元素的迭代器。
全部返回类型(iterator、const_iterator、local_iterator和const_local_iterator)都是成员类型。在unordered_map类模板中,这些是正向迭代器类型。
局部迭代器与非局部迭代器属于同一类别。它们的value_type、diffence_type、指针和引用成员类型也是相同的。可是迭代器自己不必定是相同类型的。
8)end
container iterator (1) iterator end() noexcept; const_iterator end() const noexcept;
bucket iterator (2) local_iterator end (size_type n); const_local_iterator end (size_type n) const;
返回迭代器终点
返回一个迭代器,该迭代器指向unordered_map容器(1)或其中一个桶(2)中的past-the-end元素。
end返回的迭代器不指向任何元素,而是指向unordered_map容器中最后一个元素后面的位置(它的结束位置)。所以,返回的值不能取消引用——它一般用于描述一个范围的开放端,好比[begin,end]。
注意,unordered_map对象并不保证其元素的顺序。可是,在任何状况下,从开始到结束的范围覆盖容器(或桶)中的全部元素,直到失效。
参数:
n:
桶数。这应该小于bucket_count。
它是一个可选参数,能够改变这个成员函数的行为:若是设置,迭代器将检索到该桶的第一个元素,不然它将指向容器的第一个元素。
成员类型size_type是无符号整数类型。
返回值:
元素的迭代器,遍历容器(1)或桶(2)的末端。
全部返回类型(iterator、const_iterator、local_iterator和const_local_iterator)都是成员类型。在unordered_map类模板中,这些是正向迭代器类型。
局部迭代器与非局部迭代器属于同一类别。它们的value_type、diffence_type、指针和引用成员类型也是相同的。可是迭代器自己不必定是相同类型的。
举例:
#include <iostream> #include <string> #include <unordered_map> using namespace std; int main(){ unordered_map<string, string> mymap; mymap = {{"Australia", "canberra"}, {"U.S.", "Washington"}, {"France", "Paris"}}; cout << "mymap contains : "; for(auto it = mymap.begin(); it!=mymap.end(); ++it) cout << " " << it->first << " : " << it->second; cout << endl; cout << "mymap's buckets contains: \n"; for(unsigned i = 0; i<mymap.bucket_count(); ++i){ cout << "bucket #" << i << " contains : "; for(auto local_it = mymap.begin(i); local_it!=mymap.end(i); ++local_it) cout << " " <<local_it->first << " : " << local_it->second; cout << endl; } return 0; }
返回:
mymap contains : France : Paris U.S. : Washington Australia : canberra mymap's buckets contains: bucket #0 contains : bucket #1 contains : bucket #2 contains : France : Paris bucket #3 contains : U.S. : Washington Australia : canberra bucket #4 contains :
9)cbegin
container iterator (1) const_iterator cbegin() const noexcept;
bucket iterator (2) const_local_iterator cbegin ( size_type n ) const;
返回const_iterator起点
返回一个const_iterator,指向unordered_map容器(1)或其中一个桶(2)中的第一个元素。
常量迭代器是指向const内容的迭代器。这个迭代器能够增长或减小(除非它自己也是const),就像unordered_map::begin返回的迭代器同样,可是它不能用来修改它所指向的内容。
参数:
n:
桶数。这应该小于bucket_count。
它是一个可选参数,能够改变这个成员函数的行为:若是设置,迭代器将检索到该桶的第一个元素,不然它将指向容器的第一个元素。
成员类型size_type是无符号整数类型。
返回值:
容器(1)或桶(2)中的第一个元素的迭代器。
const_iterator和const_local_iterator都是成员类型。在unordered_map类模板中,这些是正向迭代器类型。
const_local_iterator是与const_iterator相同类别的interator。它们的value_type、diffence_type、指针和引用成员类型也是相同的。可是迭代器自己不必定是相同类型的。
10)cend
container iterator (1) const_iterator cend() const noexcept;
bucket iterator (2) const_local_iterator cend ( size_type n ) const;
返回const_iterator结尾
返回一个const_iterator,指向unordered_map容器(1)或其中一个桶(2)中的past-the-end元素。
cend返回的const_iterator不指向任何元素,而是指向unordered_map容器中最后一个元素以后的位置(past-the-end),或者它的一个bucket(即,past-the-end的位置)。所以,返回的值不能取消引用——它一般用于描述范围的开放端,好比[cbegin,cend)。
注意,unordered_map对象并不保证其元素的顺序。可是,在任何状况下,从cbegin到cend的范围覆盖容器(或bucket)中的全部元素,直到失效。
const_iterator是指向const内容的迭代器。这个迭代器能够增长或减小(除非它自己也是const),就像unordered_map::end返回的迭代器同样,可是它不能用来修改它所指向的内容。
参数:
n:
桶数。这应该小于bucket_count。
它是一个可选参数,能够改变这个成员函数的行为:若是设置,迭代器将检索到该桶的第一个元素,不然它将指向容器的第一个元素。
成员类型size_type是无符号整数类型。
返回值:
元素的迭代器,遍历容器(1)或桶(2)的末端。
const_iterator和const_local_iterator都是成员类型。在unordered_map类模板中,这些是正向迭代器类型。
const_local_iterator是与const_iterator相同类别的interator。它们的value_type、diffence_type、指针和引用成员类型也是相同的。可是迭代器自己不必定是相同类型的。
举例:
#include <iostream> #include <string> #include <unordered_map> using namespace std; int main(){ unordered_map<string, string> mymap; mymap = {{"Australia", "canberra"}, {"U.S.", "Washington"}, {"France", "Paris"}}; cout << "mymap contains : "; for(auto it = mymap.cbegin(); it!=mymap.cend(); ++it) cout << " " << it->first << " : " << it->second; cout << endl; cout << "mymap's buckets contains: \n"; for(unsigned i = 0; i<mymap.bucket_count(); ++i){ cout << "bucket #" << i << " contains : "; for(auto local_it = mymap.cbegin(i); local_it!=mymap.cend(i); ++local_it) cout << " " <<local_it->first << " : " << local_it->second; cout << endl; } return 0; }
返回:
mymap contains : France : Paris U.S. : Washington Australia : canberra mymap's buckets contains: bucket #0 contains : bucket #1 contains : bucket #2 contains : France : Paris bucket #3 contains : U.S. : Washington Australia : canberra bucket #4 contains :
11)operator[]
mapped_type& operator[] ( const key_type& k ); mapped_type& operator[] ( key_type&& k );
访问元素
若是k匹配容器中元素的键,则该函数返回对其映射值的引用。
若是k不匹配容器中任何元素的键,则该函数用该键插入一个新元素并返回对其映射值的引用。请注意,即便没有将映射值分配给元素(元素是使用其默认构造函数构造的),这也老是将容器大小增长1。
相似的成员函数unordered_map::at在具备键的元素存在时具备相同的行为,但在元素不存在时抛出异常。
参数:
k:
要访问其映射值的元素的键值。
成员类型key_type是存储在容器中的元素的键的类型,在unordered_map中定义为其第一个模板参数(键key)的别名。
若是是rvalue(第二个版本,右值引用),则在插入新元素时移动键而不是复制键。
返回值:
对元素的映射值的引用,其键值等效于k。
成员类型mapped_type是容器中映射值的类型,在unordered_map中定义为其第二个模板参数(T)的别名。
若是插入了新元素,则使用allocator_traits<allocator_type>::construct()分配它的存储,这可能会在失败时抛出异常(对于默认分配器,若是分配请求没有成功,则抛出bad_alloc)。
举例:
#include <iostream> #include <string> #include <unordered_map> using namespace std; int main(){ unordered_map<string, string> mymap; mymap["Bakery"] = "Barbara"; mymap["Seafood"] = "Lisa"; mymap["Produce"] = "John"; string name = mymap["Bakery"]; mymap["Seafood"] = name; mymap["Bakery"] = mymap["Produce"]; name = mymap["Deli"]; mymap["Produce"] = mymap["Gifts"]; for(auto& x:mymap) cout << x.first << ": " << x.second << endl; return 0; }
返回:
Deli:
Produce:
Gifts:
Seafood: Barbara
Bakery: John
12)at
mapped_type& at ( const key_type& k ); const mapped_type& at ( const key_type& k ) const;
访问元素
返回对unordered_map中键为k的元素的映射值的引用。
若是k不匹配容器中任何元素的键,该函数将抛出out_of_range异常。
参数:
k:
要访问其映射值的元素的键值。
成员类型key_type是存储在容器中的元素的键的类型,在unordered_map中定义为其第一个模板参数(键key)的别名。
返回值:
对元素的映射值的引用,其键值等效于k。
成员类型mapped_type是容器中映射值的类型,在unordered_map中定义为其第二个模板参数(T)的别名。
举例:
#include <iostream> #include <string> #include <unordered_map> using namespace std; int main(){ unordered_map<string, int> mymap = { {"Mars", 3000}, {"Saturn", 60000}, {"Jupiter", 70000} }; mymap.at("Mars") = 3396; mymap.at("Saturn") += 272; mymap.at("Jupiter") = mymap.at("Saturn") + 9638; for(auto& x : mymap){ cout << x.first << ": " << x.second << endl; } return 0; }
返回:
Jupiter: 69910 Saturn: 60272 Mars: 3396
13)find
iterator find ( const key_type& k ); const_iterator find ( const key_type& k ) const;
获取元素的iterator
在容器中搜索以k为键的元素,若是找到了,就返回一个迭代器,不然就返回unordered_map::end(容器末尾的元素)的迭代器。
另外一个成员函数unordered_map::count可用于检查特定键是否存在。
还可使用at或操做符[]的成员函数直接访问映射值。
参数:
k:
要搜索的建值key。
成员类型key_type是容器中元素的键的类型,在unordered_map中定义为其第一个模板参数(key键)的别名。
返回值:
元素的迭代器(若是找到指定的键值),或者unordered_map::end(若是在容器中没有找到指定的键)。
成员类型iterator和const_iterator是前向迭代器类型。
举例:
#include <iostream> #include <string> #include <unordered_map> using namespace std; int main(){ unordered_map<string, double> mymap = { {"mom", 5.4}, {"dad", 6.1}, {"bro", 5.9} }; string input; cout << "who?"; getline(cin, input); unordered_map<string, double>::const_iterator got = mymap.find(input); if(got == mymap.end()) cout << "not found"; else cout << got->first << " is " << got->second; cout << endl; return 0; }
返回:
who?mom mom is 5.4
14)count
size_type count ( const key_type& k ) const;
用特定的键对元素计数
在容器中搜索键为k的元素,并返回找到的元素数量。由于unordered_map容器不容许重复的键,这意味着若是容器中存在具备该键的元素,则该函数实际返回1,不然返回0。
参数:
k:
要搜索的建值key。
成员类型key_type是容器中元素的键的类型,在unordered_map中定义为其第一个模板参数(key键)的别名。
返回值:
若是找到键值等于k的元素,则为1,不然为0。
成员类型size_type是无符号整数类型。
举例:
#include <iostream> #include <string> #include <unordered_map> using namespace std; int main(){ unordered_map<string, double> mymap = { {"Burger", 2.99}, {"Fries", 1.99}, {"Soda", 1.50} }; for(auto& x : {"Burger", "Pizza", "Salad", "Soda"}){ if(mymap.count(x) > 0){ cout << "mymap has " << x << endl; }else{ cout << "mymap has no " << x << endl; } } return 0; }
返回:
mymap has Burger
mymap has no Pizza
mymap has no Salad
mymap has Soda
15)equal_range
pair<iterator,iterator> equal_range ( const key_type& k ); pair<const_iterator,const_iterator> equal_range ( const key_type& k ) const;
获取具备特定键值的元素范围
返回一个范围的边界,该范围包含容器中全部的元素,其中键的值与k相比较。在unordered_map容器中,键是唯一的,该范围最多包含一个元素。
若是k与容器中的任何键不匹配,则返回的范围将end做为其上下范围边界。
参数:
k:
要搜索的建值key。
成员类型key_type是容器中元素的键的类型,在unordered_map中定义为其第一个模板参数(key键)的别名。
返回值:
函数返回一个对pair,其中它的成员pair::first是指向范围下界的迭代器,pair::second是指向其上界的迭代器。范围内的元素是这两个迭代器之间的元素,包括pair::first,而不包括pair::second。
成员类型iterator和const_iterator是前向迭代器类型。
举例:
#include <iostream> #include <string> #include <unordered_map> #include <algorithm> using namespace std; using stringmap = unordered_map<string, string>; int main(){ stringmap myumm = { {"orange", "FL"}, {"strawberry", "LA"}, {"strawberry", "OK"}, {"pumpkin", "NH"} }; cout << "Entries with strawberry : "; auto range = myumm.equal_range("strawberry"); for_each( range.first, range.second, [](stringmap::value_type& x){cout << " " << x.second;} ); return 0; }
返回:
Entries with strawberry : LA
若是使用的是unordered_multimap:
#include <iostream> #include <string> #include <unordered_map> #include <algorithm> using namespace std; using stringmap = unordered_multimap<string, string>; stringmap merge(stringmap a, stringmap b){ stringmap temp{a}; temp.insert(b.begin(), b.end()); return temp; } int main(){ stringmap myumm = { {"orange", "FL"}, {"strawberry", "LA"}, {"strawberry", "OK"}, {"pumpkin", "NH"} }; cout << "Entries with strawberry : "; auto range = myumm.equal_range("strawberry"); for_each( range.first, range.second, [](stringmap::value_type& x){cout << " " << x.second;} ); return 0; }
返回:
Entries with strawberry : LA OK
16)emplace-若是该建值已有则不操做
template <class... Args> pair<iterator, bool> emplace ( Args&&... args );
构造和插入元素
若是unordered_map的键是唯一的,则在该元素中插入一个新元素。使用args做为元素构造函数的参数构造这个新元素。
只有在容器中没有与被放置的键等价的键时才进行插入(unordered_map中的键是唯一的)。
若是插入,这将有效地将容器大小增长1。
存在一个相似的成员函数insert,它复制或将现有对象移动到容器中。
参数:
args:
参数,用于为插入的元素构造映射类型的新对象。
返回值:
若是发生插入的状况(由于不存在具备相同键值的其余元素),函数将返回一个pair对象,该对象的第一个组件是插入元素的迭代器,第二个组件为true。
不然,返回的pair对象的第一个组件是一个迭代器,它指向容器中具备相同键的元素,第二个组件是false。
成员类型迭代器是正向迭代器类型。
新元素的存储是使用allocator_traits<allocator_type>::construct()分配的,它可能会在失败时抛出异常(对于默认的分配器,若是分配请求没有成功,就会抛出bad_alloc)。
17)emplace_hint
template <class... Args> iterator emplace_hint ( const_iterator position, Args&&... args );
使用提示来构造和插入元素
若是unordered_map的键是唯一的,则在该元素中插入一个新元素。使用args做为元素构造函数的参数构造这个新元素。position位置点指向容器中的一个位置,该位置提示从何处开始搜索其插入点(容器可能使用,也可能不使用此建议来优化插入操做)。
只有在容器中没有与被放置的键等价的键时才进行插入(unordered_map中的键是唯一的)。
若是插入,这将有效地将容器大小增长1。
相似的成员函数insert也存在,它能够复制或将现有对象移动到容器中,也能够获取位置提示。
参数:
position:
做为插入操做提示的位置。容器可使用这个值来优化操做。
成员类型const_iterator是正向迭代器类型。
args:
参数,用于为插入的元素构造映射类型的新对象。
返回值:
容器中元素的迭代器,其键至关于新插入的元素。若是确实插入了新插入的元素,那么它将指向该元素;若是已经存在键,则指向具备等效键的现有元素(不替换它)。
成员类型迭代器是一个前向迭代器。
新元素的存储是使用allocator_traits<allocator_type>::construct()分配的,它可能会在失败时抛出异常(对于默认的分配器,若是分配请求没有成功,就会抛出bad_alloc)。
举例:
#include <iostream> #include <string> #include <unordered_map> #include <algorithm> using namespace std; int main(){ unordered_map<string,string> mymap; mymap.emplace("NCC-1701", "J.T. Kirk"); mymap.emplace("NCC-1701-D", "J.L. Picard"); mymap.emplace("NCC-74656", "K. Janeway"); mymap.emplace("NCC-1701", "new J.T. Kirk"); cout << "mymap contains : " << endl; for(auto& x:mymap) cout << x.first << ": " << x.second << endl; cout << endl; return 0; }
返回:
NCC-74656: K. Janeway NCC-1701-D: J.L. Picard NCC-1701: J.T. Kirk
18)insert-可同时插入多个
(1) pair<iterator,bool> insert ( const value_type& val );
(2) template <class P> pair<iterator,bool> insert ( P&& val );
(3) iterator insert ( const_iterator hint, const value_type& val );
(4) template <class P> iterator insert ( const_iterator hint, P&& val );
(5) template <class InputIterator> void insert ( InputIterator first, InputIterator last );
(6) void insert ( initializer_list<value_type> il );
插入元素
在unordered_map中插入新元素。
只有当每一个元素的键不等于容器中已经存在的任何其余元素的键时,才会插入元素(unordered_map中的键是唯一的)。
这实际上增长了插入元素的数量,从而增长了容器的大小。
这些参数决定了插入了多少个元素,并将它们初始化为哪些值:
参数:
成员类型const_iterator是正向迭代器类型。
#include <iostream> #include <string> #include <unordered_map> #include <algorithm> using namespace std; int main(){ unordered_map<string, double> myrecipe, mypantry = {{"milk", 2.0}, {"flour", 1.5}}; pair<string, double> myshopping("barking powder", 0.3); myrecipe.insert(myshopping); myrecipe.insert(make_pair<string, double>("egg", 6.0)); myrecipe.insert({{"suger", 0.8},{"salt", 0.1}}); cout << "myrecipe contains : " << endl; for(auto& x : myrecipe) cout << x.first << " " << x.second << endl; cout << endl; return 0; }
返回:
myrecipe contains : suger 0.8 egg 6 salt 0.1 barking powder 0.3
19)erase
by position (1) iterator erase ( const_iterator position ); by key (2) size_type erase ( const key_type& k ); range (3) iterator erase ( const_iterator first, const_iterator last );
删除元素
从unordered_map容器中删除单个元素或一组元素([first,last])。
经过调用每一个元素的析构函数,能够经过删除的元素数量有效地减小容器的大小。
参数:
position:
指向要从unordered_map中删除的单个元素的迭代器。
成员类型const_iterator是正向迭代器类型。
k:
要删除的建值key。
成员类型key_type是容器中元素的键的类型,在unordered_map中定义为其第一个模板参数(key键)的别名。
#include <iostream> #include <string> #include <unordered_map> #include <algorithm> using namespace std; int main(){ unordered_map<string, string> mymap; // populating container: mymap["U.S."] = "Washington"; mymap["U.K."] = "London"; mymap["France"] = "Paris"; mymap["Russia"] = "Moscow"; mymap["China"] = "Beijing"; mymap["Germany"] = "Berlin"; mymap["Japan"] = "Tokyo"; for(auto& x:mymap) cout << x.first << " " << x.second << endl; cout << endl; mymap.erase(mymap.begin()); for(auto& x:mymap) cout << x.first << " " << x.second << endl; cout << endl; mymap.erase("France"); for(auto& x:mymap) cout << x.first << " " << x.second << endl; cout << endl; mymap.erase(mymap.find("U.S."), mymap.end()); for(auto& x:mymap) cout << x.first << " " << x.second << endl; cout << endl; return 0; }
返回:
/Users/user/CLionProjects/untitled/cmake-build-debug/untitled Japan Tokyo China Beijing U.S. Washington Russia Moscow Germany Berlin France Paris U.K. London China Beijing U.S. Washington Russia Moscow Germany Berlin France Paris U.K. London China Beijing U.S. Washington Russia Moscow Germany Berlin U.K. London China Beijing Process finished with exit code 0
20)clear
void clear() noexcept;
清除内容
将删除unordered_map容器中的全部元素:调用它们的析构函数,并从容器中删除它们,使其大小为0。
参数:
none
返回值:
none
举例:
#include <iostream> #include <string> #include <unordered_map> #include <algorithm> using namespace std; int main(){ unordered_map<string,string> mymap = { {"house","maison"}, {"car","voiture"}, {"grapefruit","pamplemousse"} }; cout << "mymap contains : "; for(auto& x : mymap) cout << " " << x.first << "=" << x.second; cout << endl; mymap.clear(); mymap["hello"]="bonjour"; mymap["sun"]="soleil"; cout << "mymap contains : "; for(auto& x : mymap) cout << " " << x.first << "=" << x.second; cout << endl; return 0; }
返回:
mymap contains : grapefruit=pamplemousse car=voiture house=maison
mymap contains : sun=soleil hello=bonjour
21)swap
void swap ( unordered_map& ump );
交换内容
经过ump的内容交换容器的内容,ump是另外一个包含相同类型元素的unordered_map对象。大小可能不一样。
在调用这个成员函数以后,这个容器中的元素是在调用以前在ump中的元素,而ump中的元素是在这个容器中的元素。容器内部保存的其余对象(例如它们的hasher或key_equal对象)也被交换。
该函数在容器之间交换指向数据的内部指针,而不实际执行任何副本或对单个元素进行移动,所以不管大小,执行时间都是恒定的。
注意,全局算法函数的名称是swap。为使unordered_map类型的参数具备与此成员函数相同的行为和复杂性,将重载此全局函数。
参数:
ump:
另外一个与此类型相同的unordered_map容器对象。
返回值:
none
举例:
#include <iostream> #include <string> #include <unordered_map> #include <algorithm> using namespace std; int main(){ unordered_map<std::string,std::string> first = {{"Star Wars","G. Lucas"},{"Alien","R. Scott"},{"Terminator","J. Cameron"}}, second = {{"Inception","C. Nolan"},{"Donnie Darko","R. Kelly"}}; cout << "first contains : "; for(auto& x : first) cout << " " << x.first << "=" << x.second; cout << endl; cout << "second contains : "; for(auto& x : second) cout << " " << x.first << "=" << x.second; cout << endl; first.swap(second); cout << "first contains : "; for(auto& x : first) cout << " " << x.first << "=" << x.second; cout << endl; cout << "second contains : "; for(auto& x : second) cout << " " << x.first << "=" << x.second; cout << endl; return 0; }
返回:
first contains : Terminator=J. Cameron Alien=R. Scott Star Wars=G. Lucas second contains : Donnie Darko=R. Kelly Inception=C. Nolan first contains : Donnie Darko=R. Kelly Inception=C. Nolan second contains : Terminator=J. Cameron Alien=R. Scott Star Wars=G. Lucas
22)bucket_count
size_type bucket_count() const noexcept;
桶数
返回unordered_map容器中的桶数。
bucket是容器内部哈希表中的一个槽,根据键的哈希值将元素赋给它。
桶的数量直接影响容器的哈希表的负载因子(从而影响碰撞的几率)。容器会自动增长桶的数量,以使负载因子低于特定的阈值(其max_load_factor),从而在每次须要增长桶的数量时从新散列。
参数:
none
返回值:
当前桶的数量。
成员类型size_type是无符号整数类型。
举例:
#include <iostream> #include <string> #include <unordered_map> #include <algorithm> using namespace std; using stringmap = unordered_map<string, string>; stringmap merge(stringmap a, stringmap b){ stringmap temp{a}; temp.insert(b.begin(), b.end()); return temp; } int main(){ unordered_map<string,string> mymap = { {"house","maison"}, {"apple","pomme"}, {"tree","arbre"}, {"book","livre"}, {"door","porte"}, {"grapefruit","pamplemousse"} }; unsigned n = mymap.bucket_count(); std::cout << "mymap has " << n << " buckets.\n"; for (unsigned i=0; i<n; ++i) { cout << "bucket #" << i << " contains: "; for (auto it = mymap.begin(i); it!=mymap.end(i); ++it) cout << "[" << it->first << ":" << it->second << "] "; cout << "\n"; } return 0; }
返回:
/Users/user/CLionProjects/untitled/cmake-build-debug/untitled mymap has 11 buckets. bucket #0 contains: bucket #1 contains: [house:maison] bucket #2 contains: bucket #3 contains: [tree:arbre] bucket #4 contains: bucket #5 contains: bucket #6 contains: [grapefruit:pamplemousse] [door:porte] bucket #7 contains: bucket #8 contains: bucket #9 contains: bucket #10 contains: [apple:pomme] [book:livre] Process finished with exit code 0
23)max_bucket_count
size_type max_bucket_count() const noexcept;
返回最大桶数
返回unordered_map容器能够拥有的最大桶数。
这是因为系统约束或其库实现的限制,容器可能拥有的最大桶数。
参数:
none
返回值:
最大桶数。
成员类型size_type是无符号整数类型。
举例:
#include <iostream> #include <unordered_map> using namespace std; int main(){ unordered_map<int,int> mymap; cout << "max_size = " << mymap.max_size() << endl; cout << "max_bucket_count = " << mymap.max_bucket_count() << endl; cout << "max_load_factor = " << mymap.max_load_factor() << endl; return 0; }
返回:
max_size = 768614336404564650 max_bucket_count = 768614336404564650 max_load_factor = 1
24)bucket_size
size_type bucket_size ( size_type n ) const;
返回桶大小
返回bucket n中元素的数量。
bucket是容器内部哈希表中的一个槽,根据键的哈希值将元素赋给它。
桶中元素的数量影响访问桶中特定元素所需的时间。容器会自动增长桶的数量,以使装载因子(即平均桶大小)低于其max_load_factor。
参数:
n:
桶数。
这应该低于bucket_count。
成员类型size_type是无符号整数类型。
返回值:
桶中元素的数目n。
成员类型size_type是无符号整数类型。
举例:
#include <iostream> #include <string> #include <unordered_map> #include <algorithm> using namespace std; int main(){ unordered_map<string,string> mymap = { {"us","United States"}, {"uk","United Kingdom"}, {"fr","France"}, {"de","Germany"} }; unsigned nbuckets = mymap.bucket_count(); cout << "mymap has " << nbuckets << " buckets : " << endl; for(unsigned i = 0; i<nbuckets; ++i) cout << "bucket # " << i << " has " << mymap.bucket_size(i) << " elements " << endl; return 0; }
返回:
mymap has 5 buckets : bucket # 0 has 1 elements bucket # 1 has 0 elements bucket # 2 has 0 elements bucket # 3 has 1 elements bucket # 4 has 2 elements
25)bucket
size_type bucket ( const key_type& k ) const;
定位元素的桶
返回键为k的元素所在的桶号。
bucket是容器内部哈希表中的一个槽,根据键的哈希值将元素赋给它。桶的编号从0到(bucket_count-1)。
能够经过unordered_map::begin和unordered_map::end返回的范围迭代器访问bucket中的各个元素。
参数:
k:
要定位其存储桶的键值key。
成员类型key_type是容器中元素的键的类型,在unordered_map中定义为其第一个模板参数(key键)的别名。
返回值:
对应k的桶序列号
成员类型size_type是无符号整数类型。
举例:
#include <iostream> #include <string> #include <unordered_map> using namespace std; int main(){ unordered_map<string,string> mymap = { {"us","United States"}, {"uk","United Kingdom"}, {"fr","France"}, {"de","Germany"} }; for (auto& x: mymap) { cout << "Element [" << x.first << ":" << x.second << "]"; cout << " is in bucket #" << mymap.bucket (x.first) << endl; } return 0; }
返回:
Element [de:Germany] is in bucket #0 Element [fr:France] is in bucket #3 Element [uk:United Kingdom] is in bucket #4 Element [us:United States] is in bucket #4
26) load_factor
float load_factor() const noexcept;
返回负载因子
返回unordered_map容器中的当前负载因子。
负载因子是容器中元素的数量(其大小)与桶的数量(bucket_count)之间的比率:
load_factor = size / bucket_count
在哈希表中,负载因素影响碰撞的几率(即两个元素位于同一桶中的几率)。容器会自动增长桶的数量,以将负载因子保持在特定的阈值(其max_load_factor)之下,从而在每次须要扩展时致使从新散列。
要检索或更改此阈值,请使用成员函数max_load_factor。
参数:
none
返回值:
当前的负载因子
举例:
#include <iostream>
#include <string> #include <unordered_map> using namespace std; int main(){ unordered_map<string,string> mymap = { {"us","United States"}, {"uk","United Kingdom"}, {"fr","France"}, {"de","Germany"} }; cout << "size = " << mymap.size() << endl; cout << "bucket_count = " << mymap.bucket_count() << endl; cout << "load_factor = " << mymap.load_factor() << endl; cout << "max_load_factor = " << mymap.max_load_factor() << endl; return 0; }
返回:
size = 4 bucket_count = 5 load_factor = 0.8 max_load_factor = 1
27)max_load_factor
get (1) float max_load_factor() const noexcept; set (2) void max_load_factor ( float z );
获取或设置最大负载因子
第一个版本(1)返回unordered_map容器的当前最大负载因子。
第二个版本(2)将z设置为unordered_map容器的新最大负载因子。
负载因子是容器中元素的数量(其大小)与桶的数量(bucket_count)之间的比率。
默认状况下,unordered_map容器的max_load_factor为1.0。
在哈希表中,负载因素影响碰撞的几率。,两个元素位于同一桶中的几率)。容器使用max_load_factor的值做为阈值,该阈值强制增长桶的数量(从而致使从新散列)。
可是请注意,该实现可能会对桶的数量施加上限(请参阅max_bucket_count),这可能会迫使容器忽略max_load_factor。
参数:
z:
新的最大负载因子
返回值:
当前的负载因子(仅对第一个get版本)
举例:
#include <iostream> #include <string> #include <unordered_map> using namespace std; int main(){ unordered_map<string,string> mymap = { {"Au","gold"}, {"Ag","Silver"}, {"Cu","Copper"}, {"Pt","Platinum"} }; cout << "current max_load_factor: " << mymap.max_load_factor() << endl; cout << "current size: " << mymap.size() << endl; cout << "current bucket_count: " << mymap.bucket_count() << endl; cout << "current load_factor: " << mymap.load_factor() << endl; float z = mymap.max_load_factor(); mymap.max_load_factor ( z / 2.0 ); //小于当前的负载因子则设为当前的,即为0.8,而不是0.5 cout << "[max_load_factor halved]" << endl; cout << endl; cout << "new max_load_factor: " << mymap.max_load_factor() << endl; cout << "new size: " << mymap.size() << endl; cout << "new bucket_count: " << mymap.bucket_count() << endl; cout << "new load_factor: " << mymap.load_factor() << endl; mymap.max_load_factor ( 0.9 ); cout << endl; cout << "new max_load_factor: " << mymap.max_load_factor() << endl; cout << "new size: " << mymap.size() << endl; cout << "new bucket_count: " << mymap.bucket_count() << endl; cout << "new load_factor: " << mymap.load_factor() << endl; return 0; }
返回:
/Users/user/CLionProjects/untitled/cmake-build-debug/untitled current max_load_factor: 1 current size: 4 current bucket_count: 5 current load_factor: 0.8 [max_load_factor halved] new max_load_factor: 0.8 new size: 4 new bucket_count: 5 new load_factor: 0.8 new max_load_factor: 0.9 new size: 4 new bucket_count: 5 new load_factor: 0.8 Process finished with exit code 0
28)rehash
void rehash( size_type n );
设置桶数
将容器中的桶数设置为n或更多。
若是n大于容器中当前桶的数量(bucket_count),则强制从新散列。新的bucket count能够等于或大于n。
若是n小于容器中桶的当前数量(bucket_count),则该函数可能对桶数没有影响,也可能不会强制从新散列。
rehash是对哈希表的重构:容器中的全部元素根据它们的哈希值从新排列到新的bucket集合中。这可能会改变容器内元素迭代的顺序。
每当容器的负载因子在操做中超过max_load_factor时,容器就会自动执行重列。
注意,这个函数的参数是桶的数量。存在一个相似的函数unordered_map::reserve,它指望容器中的元素数量做为参数。
参数:
n:
容器哈希表的最小桶数。
成员类型size_type是无符号整数类型。
返回值:
none
举例:
#include <iostream> #include <string> #include <unordered_map> using namespace std; int main(){ unordered_map<string,string> mymap; cout << "current bucket_count: " << mymap.bucket_count() << endl; mymap.rehash(20); mymap["house"] = "maison"; mymap["apple"] = "pomme"; mymap["tree"] = "arbre"; mymap["book"] = "livre"; mymap["door"] = "porte"; mymap["grapefruit"] = "pamplemousse"; cout << "current bucket_count: " << mymap.bucket_count() << endl; return 0; }
返回:
current bucket_count: 0 current bucket_count: 23 //结果可能大于或等于20
29)reserve
void reserve ( size_type n );
请求更改每一个桶的容量
将容器中的桶数(bucket_count)设置为最适合包含至少n个元素。
若是n大于当前的bucket_count乘以max_load_factor,则容器的bucket_count将增长,并强制从新散列。
若是n小于这个值,函数可能没有做用。
参数:
n:
所要求的元素数量为最小容量。
成员类型size_type是无符号整数类型
举例:
#include <iostream> #include <string> #include <unordered_map> #include <algorithm> using namespace std; int main(){ unordered_map<string,string> mymap; mymap["house"] = "maison"; mymap["apple"] = "pomme"; mymap["tree"] = "arbre"; mymap["book"] = "livre"; mymap["door"] = "porte"; mymap["grapefruit"] = "pamplemousse"; cout << "current bucket_count: " << mymap.bucket_count() << endl; cout << "current max_load_factor: " << mymap.max_load_factor() << endl; for(unsigned i = 0; i<mymap.bucket_count(); ++i) cout << "current bucket_size: " << mymap.bucket_size(i) << endl; mymap.reserve(6); //n小于bucket_count*max_load_factor时 for (auto& x: mymap) { std::cout << x.first << ": " << x.second << std::endl; } cout << "current bucket_count: " << mymap.bucket_count() << endl; cout << "current max_load_factor: " << mymap.max_load_factor() << endl; for(unsigned i = 0; i<mymap.bucket_count(); ++i) cout << "current bucket_size: " << mymap.bucket_size(i) << endl; mymap.reserve(15);//n大于bucket_count*max_load_factor时 for (auto& x: mymap) { std::cout << x.first << ": " << x.second << std::endl; } cout << "current bucket_count: " << mymap.bucket_count() << endl; cout << "current max_load_factor: " << mymap.max_load_factor() << endl; for(unsigned i = 0; i<mymap.bucket_count(); ++i) cout << "current bucket_size: " << mymap.bucket_size(i) << endl; return 0; }
返回:
/Users/user/CLionProjects/untitled/cmake-build-debug/untitled current bucket_count: 11 current max_load_factor: 1 current bucket_size: 0 current bucket_size: 1 current bucket_size: 0 current bucket_size: 1 current bucket_size: 0 current bucket_size: 0 current bucket_size: 2 current bucket_size: 0 current bucket_size: 0 current bucket_size: 0 current bucket_size: 2 apple: pomme book: livre tree: arbre house: maison grapefruit: pamplemousse door: porte current bucket_count: 7 current max_load_factor: 1 current bucket_size: 0 current bucket_size: 1 current bucket_size: 1 current bucket_size: 2 current bucket_size: 0 current bucket_size: 1 current bucket_size: 1 apple: pomme grapefruit: pamplemousse book: livre tree: arbre house: maison door: porte current bucket_count: 17 current max_load_factor: 1 current bucket_size: 0 current bucket_size: 0 current bucket_size: 0 current bucket_size: 0 current bucket_size: 0 current bucket_size: 0 current bucket_size: 0 current bucket_size: 0 current bucket_size: 0 current bucket_size: 2 current bucket_size: 2 current bucket_size: 0 current bucket_size: 0 current bucket_size: 0 current bucket_size: 0 current bucket_size: 1 current bucket_size: 1 Process finished with exit code 0
经过使用unordered_map容器的指望大小(=6)来调用reserve,咱们避免了容器大小的增长可能产生的屡次重复哈希,并优化了哈希表的大小。
30)hash_function
hasher hash_function() const;
获得哈希函数
返回unordered_map容器使用的散列函数对象。
散列函数是一个一元函数,它接受类型为key_type的对象做为参数,并基于它返回类型为size_t的唯一值。它被构造中的容器所采用(有关更多信息,请参见unordered_map的构造函数unordered_map's constructor)。默认状况下,它是对应键类型的默认哈希函数:hash<key_type>。
参数:
none
返回值:
哈希函数。
成员类型hasher是容器使用的散列函数的类型,在unordered_map中定义为其第三个模板参数(Hash散列)的别名。
举例:
#include <iostream> #include <string> #include <unordered_map> using namespace std; using stringmap = unordered_map<string, string>; int main(){ stringmap mymap; stringmap::hasher fn = mymap.hash_function(); // 使用该hash函数求hash值 cout << "this: " << fn ("this") << endl; cout << "thin: " << fn ("thin") << endl; return 0; }
返回:
this: 1610705967341725939 thin: 3142588895646935236
31)key_eq
key_equal key_eq() const;
获得键等价谓词
返回unordered_map容器使用的键等价比较谓词。
键等价比较是一个谓词,它接受键类型的两个参数,并返回一个bool值,该值指示是否定为它们是等价的。它被构造中的容器所采用(有关更多信息,请参见unordered_map的构造函数)。默认状况下,它是equal_to<key_type>,返回的结果与将equal-to操做符(==)应用于参数相同。
参数:
none
返回值:
键相等比较对象。
成员类型key_equal是容器使用的键相等比较谓词的类型,在unordered_map中定义为其第四个模板参数(Pred)的别名。
举例:
#include <iostream> #include <string> #include <unordered_map> using namespace std; int main(){ unordered_map<string,string> mymap; bool case_insensitive = mymap.key_eq()("test","TEST"); //判断是否大小写敏感 cout << "mymap.key_eq() is "; cout << ( case_insensitive ? "case insensitive" : "case sensitive" ); cout << endl; return 0; }
返回:
mymap.key_eq() is case sensitive
可见是大小写敏感的,说明返回的是false
32)get_allocator
allocator_type get_allocator() const noexcept;
获得分配器
返回用于构造容器的分配器对象。
参数:
none
返回值:
分配器。
成员类型allocator_type是容器使用的分配器的类型,在unordered_map中定义为其第五个模板参数(Alloc)的别名。
33)operators
equality (1) template <class Key, class T, class Hash, class Pred, class Alloc> bool operator== ( const unordered_map<Key,T,Hash,Pred,Alloc>& lhs, const unordered_map<Key,T,Hash,Pred,Alloc>& rhs ); inequality (2) template <class Key, class T, class Hash, class Pred, class Alloc> bool operator!= ( const unordered_map<Key,T,Hash,Pred,Alloc>& lhs, const unordered_map<Key,T,Hash,Pred,Alloc>& rhs );
unordered_map的关系运算符
这些重载的全局操做符函数在unordered_map容器lhs和rhs之间执行适当的相等或不等比较操做。
平等比较的程序以下(若是程序找到一个结论性的答案,则在任何点中止):
首先,比较大小。
而后,在一个容器中查找另外一个容器中的每一个键,若是找到了,就比较它们的值。
注意,unordered_map::hash_function和unordered_map::key_eq对象在lhs和rhs中具备相同的行为。
参数:
举例:
#include <iostream> #include <string> #include <unordered_map> using namespace std; using stringmap = unordered_map<string, string>; int main(){ stringmap a = { {"AAPL","Apple"}, {"MSFT","Microsoft"}, {"GOOG","Google"} }; stringmap b = { {"MSFT","Microsoft"}, {"GOOG","Google"}, {"AAPL","Apple"} }; stringmap c = { {"MSFT","Microsoft Corp."}, {"GOOG","Google Inc."}, {"AAPL","Apple Inc."} }; if (a==b) cout << "a and b are equal\n"; if (b!=c) cout << "b and c are not equal\n"; return 0; }
返回:
a and b are equal
b and c are not equal
34)swap
template <class Key, class T, class Hash, class Pred, class Alloc> void swap ( unordered_map<Key,T,Hash,Pred,Alloc>& lhs, unordered_map<Key,T,Hash,Pred,Alloc>& rhs );
交换两个unordered_map容器的内容
容器lhs的内容与rhs的内容进行交换。两个容器对象必须具备相同的类型(相同的模板参数),尽管大小可能不一样。
调用这个函数以后,lhs中的元素是调用以前在rhs中的元素,rhs中的元素是在lhs中的元素。容器内部保存的其余对象(例如它们的hasher或key_equal对象)也被交换。
这是通用算法swap的一种特殊化,它经过交换内部的数据指针来提升性能,而不须要对单个元素执行任何复制或移动。
参数:
举例:
#include <iostream> #include <string> #include <unordered_map> using namespace std; using stringmap = unordered_map<string, string>; int main(){ stringmap first = {{"Star Wars","G. Lucas"},{"Alien","R. Scott"},{"Terminator","J. Cameron"}}, second = {{"Inception","C. Nolan"},{"Donnie Darko","R. Kelly"}}; cout << "first: "; for (auto& x: first) cout << x.first << " (" << x.second << "), "; cout << endl; cout << "second: "; for (auto& x: second) cout << x.first << " (" << x.second << "), "; cout << endl; swap(first,second); cout << "first: "; for (auto& x: first) cout << x.first << " (" << x.second << "), "; cout << endl; cout << "second: "; for (auto& x: second) cout << x.first << " (" << x.second << "), "; cout << endl; return 0; }
返回:
first: Terminator (J. Cameron), Alien (R. Scott), Star Wars (G. Lucas),
second: Donnie Darko (R. Kelly), Inception (C. Nolan),
first: Donnie Darko (R. Kelly), Inception (C. Nolan),
second: Terminator (J. Cameron), Alien (R. Scott), Star Wars (G. Lucas),