其他分享
首页 > 其他分享> > unordered_map使用详解

unordered_map使用详解

作者:互联网

STL:unordered_map使用笔记

参考网址:

1.概述

unordered_map的模板定义如下:

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是一种关联容器,存储基于键值和映射组成的元素,即key-value。允许基于键快速查找元素。在unordered_map中,键值唯一标识元素,映射的值是一个与该对象关联的内容的对象。

对于有序和无序性:

而内部存储结构也决定了unordered_map和map在某些特性上的不同:

无序映射实现了直接访问操作符(operator[]),该操作符允许使用其键值作为参数直接访问映射值。容器中的迭代器至少是前向迭代器forward iterators

2.属性

2.1 关联性

关联容器中的元素由他们的键引用,而不是由他们在容器中的绝对位置引用。

2.2 无序性

无序容器使用散列表来组织它们的元素,散列表允许通过它们的键快速访问元素。

2.3 Map映射

每个元素将一个键key与一个映射值value相关联:键意味着标识其主要内容是映射值的元素。

2.4 key的唯一性

在容器中没有两个元素有相同的key

2.5 Allocator-aware

容器使用一个分配器对象来动态地处理它的存储需求。

3.模板参数

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;

4.成员函数

4.1 构造函数与初始化

(1)模板类的默认构造函数,创建空的unordered_map

unordered_map<int, string> umap;

(2)使用初始化列表初始化

unordered_map<int, string> umap = unordered_map<int, string>({{1,"a"},{2,"b"}});  // 显式调用C++的构造函数
unordered_map<int, string> umap2({{3,"c"},{4,"d"}});	// 隐式调用构造函数,更简洁
unordered_map<string, string> umap{
    {"淘宝","https://www.taobao.com/"},
    {"京东","https://www.jd.com/"},
    {"天猫商城","https://jx.tmall.com/"} };

(3)拷贝构造函数初始化

// 拷贝构造函数 
unordered_map<int, string> umap4(umap3);

(4)迭代器初始化

// range
unordered_map<int, string> umap5(umap1.begin(), umap1.end());

(5)拷贝初始化

typedef std::unordered_map<std::string,std::string> stringmap;
first = {{"AAPL","Apple"},{"MSFT","Microsoft"}};  // init list
second = {{"GOOG","Google"},{"ORCL","Oracle"}};   // init list
third = merge(first,second);                      // move
first = third;                                    // copy	

4.2 capacity

(1)是否为空

cout << "first " << (first.empty() ? "is empty" : "is not empty") << endl;
cout << "first " << (second.empty() ? "is empty" : "is not empty") << endl;

(2)目前容量

cout << "thrid.size is" << third.size() << endl;

4.3 迭代

/**  Iteration **/
for (auto it = first.begin(); it != first.end(); it++){
    cout << " " << it->first << ":" << it->second;
}
cout<<endl;

4.4 元素的访问

(1)operator[]

first["GOOG"] = "Google";		// new element inserted
first["AAPL"] = "Apple";		// new element inserted
first["MSFT"] = "Microsoft";	// new element inserted
first["BOB"] = "Bob";
string brand1 = first["GOOG"];	// read
first["BOB"] = "";				// writen
for (auto it = first.begin(); it != first.end(); it++){
    cout << " " << it->first << ":" << it->second;
}
cout<<endl;

(2)at

unrdered_map<string,int> mymap = {
    {"Mars", 3000},
    {"Saturn", 60000},
    {"Jupiter", 70000}};
mymap.at("Mars") = 3396;
mymap.at("Saturn") += 127;
mymap.at("Jupiter") = mymap.at("Saturn") + 9638;
	
for (auto& x: mymap) {
    std::cout << x.first << ": " << x.second << std::endl;
}

4.5 元素的查找

(1)find

find函数的原型如下:

iterator find ( const key_type& k );
const_iterator find ( const key_type& k ) const;

find函数可以用来获取元素的迭代器:

std::unordered_map<std::string,double> mymap1 = {
	    {"mom",5.4},
	    {"dad",6.1},
	    {"bro",5.9} };
	
string person = "dad";
unordered_map<std::string,double>::const_iterator got = mymap1.find(person);

if(got == mymap1.end()){
    cout << "not found" << endl;
}else{
    cout << got->first << " is " << got->second << endl; 
}

注意迭代器的声明方式:

unordered_map<string,double>::const_iterator

(2)count

成员函数count判断集合中有没有键值k,函数原型如下:

size_type count ( const key_type& k ) const;

其中size_type是一个整型变量,如果无序map中有key k,那么返回值为1,否则返回值为0。

4.6 修改

(1)元素的插入

(2)元素的删除

元素的删除使用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 );
//erase
std::unordered_map<std::string,std::string> mymap3;

// populating container:
mymap3["U.S."] = "Washington";
mymap3["U.K."] = "London";
mymap3["France"] = "Paris";
mymap3["Russia"] = "Moscow";
mymap3["China"] = "Beijing";
mymap3["Germany"] = "Berlin";
mymap3["Japan"] = "Tokyo";

cout << endl;
cout << "------------------------------------------" << endl;
// 根据位置,删除第一个:
mymap3.erase(mymap3.begin());
// 根据key
mymap3.erase("France");
// range
mymap3.erase(mymap3.find("Germany"), mymap3.end());

for (auto& x : mymap3) {
    cout << x.first << ":" << x.second << " ";
}

标签:map,const,详解,key,type,unordered,first
来源: https://blog.csdn.net/weixin_45745854/article/details/122785542