构造函数
-
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() );
示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
// constructing unordered_maps
#include <iostream>
#include <string>
#include <unordered_map>
typedef std::unordered_map<std::string,std::string> stringmap;
stringmap merge (stringmap a,stringmap b) {
stringmap temp(a); temp.insert(b.begin(),b.end()); return temp;
}
int main ()
{
stringmap first; // empty
stringmap second ( {{"apple","red"},{"lemon","yellow"}} ); // init list
stringmap third ( {{"orange","orange"},{"strawberry","red"}} ); // init list
stringmap fourth (second); // copy
stringmap fifth (merge(third,fourth)); // move
stringmap sixth (fifth.begin(),fifth.end()); // range
std::cout << "sixth contains:";
for (auto& x: sixth) std::cout << " " << x.first << ":" << x.second;
std::cout << std::endl;
return 0;
}
|
赋值运算符
copy (1)
unordered_set& operator= ( const unordered_set& ust );
move (2)
unordered_set& operator= ( unordered_set&& ust );
initializer list (3)
unordered_set& operator= ( intitializer_list<value_type> il );
示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
// unordered_set::operator=
#include <iostream>
#include <string>
#include <unordered_set>
template<class T>
T cmerge (T a, T b) {
T t(a); t.insert(b.begin(),b.end()); return t;
}
int main ()
{
std::unordered_set<std::string> first, second, third;
first = {"red","green","blue"}; // init list
second = {"orange","pink","yellow"}; // init list
third = cmerge (first, second); // move
first = third; // copy
std::cout << "first contains:";
for (const std::string& x: first) std::cout << " " << x;
std::cout << std::endl;
return 0;
}
|
容量

迭代器

元素访问

元素查找

修改

emplace
template <class... Args>
pair<iterator, bool> emplace ( Args&&... args );
构造和插入元素
如果其键是唯一的 ,则在unordered_map中插入一个新元素。这个新元素是使用args作为元素构造函数的参数构建的。
仅当容器中没有元素具有与放置的元素相同的键(unordered_map中的键是唯一的)时才会进行插入。
示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
#include <iostream>
#include <string>
#include <unordered_map>
int main ()
{
std::unordered_map<std::string,std::string> mymap;
mymap.emplace ("NCC-1701", "J.T. Kirk");
mymap.emplace ("NCC-1701-D", "J.L. Picard");
mymap.emplace ("NCC-74656", "K. Janeway");
std::cout << "mymap contains:" << std::endl;
for (auto& x: mymap)
std::cout << x.first << ": " << x.second << std::endl;
std::cout << std::endl;
return 0;
}
|
emplace_hint函数
template <class... Args>
iterator emplace_hint ( const_iterator position, Args&&... args );
用提示构造和插入元素
如果其键是唯一的 ,则在unordered_map中插入一个新元素。这个新元素是使用args作为元素构造函数的参数构建的。位置指向容器中的某个位置,建议您在何处开始搜索其插入点(容器可能使用或可能不会使用此建议来优化插入操作)。
仅当容器中没有元素具有与放置的元素相同的键(unordered_map中的键是唯一的)时才会进行插入。
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中的键是唯一的)时才会被插入。
示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
// unordered_map::insert
#include <iostream>
#include <string>
#include <unordered_map>
int main ()
{
std::unordered_map<std::string,double>
myrecipe,
mypantry = {{"milk",2.0},{"flour",1.5}};
std::pair<std::string,double> myshopping ("baking powder",0.3);
myrecipe.insert (myshopping); // copy insertion
myrecipe.insert (std::make_pair<std::string,double>("eggs",6.0)); // move insertion
myrecipe.insert (mypantry.begin(), mypantry.end()); // range insertion
myrecipe.insert ( {{"sugar",0.8},{"salt",0.1}} ); // initializer list insertion
std::cout << "myrecipe contains:" << std::endl;
for (auto& x: myrecipe)
std::cout << x.first << ": " << x.second << std::endl;
std::cout << std::endl;
return 0;
}
|
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))。
这有效地减少了移除元素数量的容器大小,调用每个元素的析构函数。
clear函数
void clear() noexcept;
示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
#include <iostream>
#include <string>
#include <unordered_map>
int main ()
{
std::unordered_map<std::string,std::string> mymap =
{ {"house","maison"}, {"car","voiture"}, {"grapefruit","pamplemousse"} };
std::cout << "mymap contains:";
for (auto& x: mymap) std::cout << " " << x.first << "=" << x.second;
std::cout << std::endl;
mymap.clear();
mymap["hello"]="bonjour";
mymap["sun"]="soleil";
std::cout << "mymap contains:";
for (auto& x: mymap) std::cout << " " << x.first << "=" << x.second;
std::cout << std::endl;
return 0;
}
|
swap函数
void swap ( unordered_map& ump );
示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
// unordered_map::swap
#include <iostream>
#include <string>
#include <unordered_map>
int main ()
{
std::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"}};
first.swap(second);
std::cout << "first: ";
for (auto& x: first) std::cout << x.first << " (" << x.second << "), ";
std::cout << std::endl;
std::cout << "second: ";
for (auto& x: second) std::cout << x.first << " (" << x.second << "), ";
std::cout << std::endl;
return 0;
}
|
桶

hash
