博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
stl:map用法_C ++ STL无序映射– std :: unordered_map
阅读量:2508 次
发布时间:2019-05-11

本文共 8488 字,大约阅读时间需要 28 分钟。

stl:map用法

In this tutorial you will learn about stl unordered map container i.e. std::unordered_map and all functions applicable on it.

在本教程中,您将学习stl无序映射容器,即std :: unordered_map及其适用的所有函数。

By its name we can say that it comes under Associative containers with Unordered property. We know that any unordered container internally implemented with hash tables. So same has hashing concept in worst case any operation takes O(n) time and in average and best case it takes O(1) time. We can say it vary based on type of hash function we used. With comparison to map containers these work efficiently to find value by using key. And one main difference is that map elements are order but unordered map elements are not in order. Since it is based on key-value pair concept all keys must be unique.

顾名思义,它可以归为具有Unordered属性的关联容器。 我们知道任何内部使用哈希表实现的无序容器。 因此,在最坏的情况下,任何操作都需要O(n)时间,而在平均的最佳情况下,则需要O(1)时间,因此具有哈希概念。 我们可以说它根据我们使用的哈希函数的类型而变化。 与地图容器相比,这些容器可以有效地使用键来寻找价值。 主要区别在于,地图元素是有序的,而无序地图元素不是有序的。 由于它基于键值对概念,因此所有键都必须是唯一的。

C ++ STL无序映射– std :: unordered_map (C++ STL Unordered Map – std::unordered_map)

Iterators that can be applicable on Unordered map:

可以在无序地图上应用的迭代器:

begin(): returns iterator to the beginning

begin():将迭代器返回到开头

end(): returns iterator to the end of the container.

end():将迭代器返回到容器的末尾。

cbegin(): Returns constant iterator to the beginning.

cbegin():将常数迭代器返回到开头。

cend(): Returns constant iterator to the end.

cend():将常量迭代器返回到末尾。

To work with unordered map we can separately include unordered_map header file. That is:

要使用无序地图,我们可以单独包含unordered_map头文件。 那是:

#include <unordered_map>

#include <unordered_map>

Functions applicable on unordered map:

适用于无序地图的功能:

Inserting and printing:

插入和打印:

Here we can insert using index operator []. Main advantage of these key-value type of containers is to store relevant information.

在这里,我们可以使用索引运算符[]进行插入。 这些容器的键值类型的主要优点是可以存储相关信息。

Example:

例:

Andhra – Amaravathi

安得拉–阿马拉瓦西

Telangana – Hyderabad

特伦甘纳邦–海得拉巴

Odisha – Bhubaneswar

奥里萨邦–布巴内斯瓦尔

Like this we can store these type of information. Here state names are keys and their capitals are values.

这样,我们可以存储这些类型的信息。 这里的州名是键,大写的是值。

Or we can compare these things with dictionaries in python. For simply understanding and to show all functions on these unordered map  here I am using keys 0, 1, 2, . . . n. And values are user choice. But remember both key and value can be different data types.

或者我们可以将这些东西与python中的字典进行比较。 为了简单地理解和显示这些无序映射上的所有功能,我在这里使用键0、1、2,.。 。 。 。 价值观是用户的选择。 但是请记住,键和值可以是不同的数据类型。

Method 1: We can insert using based on key.

方法1:我们可以基于键插入使用。

Ex: unMap[key] = value;

例如: unMap [key] =值;

Method 2: Using insert() function.

方法2:使用insert()函数。

Using this we can directly include key and value pair at a time. To say that those are pair, we should use make_pair function.

使用此方法,我们可以一次直接包含键和值对。 要说那是一对,我们应该使用make_pair函数。

Since container storing key – value pairs, we use combination of iterators and pointers (“first” for key and “second” for value).

由于容器存储键-值对,因此我们使用迭代器和指针的组合(“第一”代表键,“第二”代表值)。

Below example code explains these clearly:

下面的示例代码清楚地解释了这些:

#include 
#include
 using namespace std; int main(){ int value; unordered_map
unMap; // Declearing unordered_map
:: iterator it; for (int i=0; i<5; i++) { cout << "Enter value for key " << i << endl; cin >> value; unMap [i]= value; // inserting using key } // inserting using .insert function unMap.insert(make_pair(10,100)); cout << "(Key , Value)" << endl; for (it= unMap.begin(); it!= unMap.end(); ++it) { cout << "(" << it->first << " , " << it->second << ")" << endl; } return 0;}

Output

输出量

Enter value for key 0 10 Enter value for key 1 20 Enter value for key 2 30 Enter value for key 3 40 Enter value for key 4 50 (Key , Value) (10 , 100) (4 , 50) (0 , 10) (1 , 20) (2 , 30) (3 , 40)

输入按键0的 10 输入按键1的 20 输入按键2的 30 输入按键3的 40 输入按键4的值 50 (按键,值) (10,100) (4,50) (0,10) (1,20) (2,30) (3,40)

Other modification functions on unordered map:

无序图上的其他修改功能:

erase(): We can remove using key. Then key and corresponding value both will be deleted. And by pointing iterator to some pair, we can delete that.

delete():我们可以使用键删除。 然后,键和对应的值都将被删除。 通过将迭代器指向某个对,我们可以删除它。

swap(): swaps elements of Unordered map1 to Unordered map2 and vice-versa. Swapping can be done successfully even both containers have different sizes.

swap():将Unordered map1的元素交换为Unordered map2,反之亦然。 即使两个容器的大小不同,交换也可以成功完成。

clear(): removes all elements in the Unordered map. It results container size to 0.

clear():删除无序映射中的所有元素。 结果容器大小为0。

Example program to show above function:

显示上述功能的示例程序:

#include 
#include
 using namespace std; int main(){ unordered_map
unMap; unordered_map
:: iterator it; for (int i=0; i<5; i++) { unMap [i]= i+10; } cout << "key - value pairs in unordered map are" << endl; cout << "(Key , Value)" << endl; for (it= unMap.begin(); it!= unMap.end(); ++it) { cout << "(" << it->first << " , " << it->second << ")" << endl; } cout << "Erasing pair whcih has key value 3" << endl; unMap.erase(3); it= unMap.begin(); cout << "Erasing first pair" << endl; unMap.erase(it); cout << "Remaining pairs are" << endl; cout << "(Key , Value)" << endl; for (it= unMap.begin(); it!= unMap.end(); ++it) { cout << "(" << it->first << " , " << it->second << ")" << endl; } // creating new unordered map unordered_map
unMap2; for(int i=0; i<5; i++) { unMap2[i]= i+100; } cout << "New unordered map elements are " << endl; cout << "(Key , Value)" << endl; for (it= unMap2.begin(); it!= unMap2.end(); ++it) { cout << "(" << it->first << " , " << it->second << ")" << endl; } cout << "Performing swap operation......." << endl; unMap.swap(unMap2); cout << "After swap operation second unordered map is " << endl; cout << "(Key , Value)" << endl; for (it= unMap2.begin(); it!= unMap2.end(); ++it) { cout << "(" << it->first << " , " << it->second << ")" << endl; } cout << "After swap operation unMap1 is " << endl; cout << "(Key , Value)" << endl; for (it= unMap.begin(); it!= unMap.end(); ++it) { cout << "(" << it->first << " , " << it->second << ")" << endl; } // clear operation cout << "Applying clear operation on unMap2" << endl; unMap2.clear(); cout << "Now unMap2 is " << endl; cout << "(Key , Value)" << endl; for (it= unMap2.begin(); it!= unMap2.end(); ++it) { cout << "(" << it->first << " , " << it->second << ")" << endl; } return 0;}

Output

输出量

key – value pairs in unordered map are (Key , Value) (4 , 14) (0 , 10) (1 , 11) (2 , 12) (3 , 13) Erasing pair whcih has key value 3 Erasing first pair Remaining pairs are (Key , Value) (0 , 10) (1 , 11) (2 , 12) New unordered map elements are (Key , Value) (4 , 104) (0 , 100) (1 , 101) (2 , 102) (3 , 103) Performing swap operation……. After swap operation second unordered map is (Key , Value) (0 , 10) (1 , 11) (2 , 12) After swap operation unMap1 is (Key , Value) (4 , 104) (0 , 100) (1 , 101) (2 , 102) (3 , 103) Applying clear operation on unMap2 Now unMap2 is (Key , Value)

键-在无序映射值对是 (键,值)(4,14)(0,10)(1,11)(2,12)(3,13) 擦除对whcih具有键值3 删除第一对 剩余对是 (Key,Value) (0,10) (1,11) (2,12) 新的无序映射元素是 (Key,Value) (4,104) (0,100) (1,101) (2,102 ) (3,103) 执行交换操作……。 在交换操作之后,第二个无序映射是 (Key,Value) ( 0,10 ) ( 1,11) (2,12) 在交换操作之后,unMap1是 (Key,Value) (4,104) ( 0,100 ) (1, 101) (2,102) (3,103) 在unMap2上应用清除操作 现在unMap2是 (Key,Value)

Functions related to capacity:

与容量有关的功能:

empty(): returns a Boolean value whether unordered_map is empty or not.

empty():无论unordered_map是否为空,都返回一个布尔值。

size(): returns present size of the container.

size():返回容器的当前大小。

max_size(): returns the maximum size a map can have.

max_size():返回地图可以具有的最大尺寸。

#include
#include
 using namespace std; int main(){ unordered_map
unMap; unordered_map
:: iterator it; for (int i=0; i<5; i++) { unMap [i]= i+10; } cout << "Size of the unordered map is " << unMap.size() << endl; cout << "Maximum Size of the unordered map is " << unMap.max_size() << endl; bool check= unMap.empty(); if (check) cout << "Unordered map is empty" << endl; else cout << "Unordered map is not empty" << endl; return 0;}

Output

输出量

Size of the unordered map is 5 Maximum Size of the unordered map is 1152921504606846975 Unordered map is not empty

无序图的 大小 为5 最大 无序图的 大小为1152921504606846975 无序图不为空

Comment below if you have any queries related to stl unordered map.

如果您有关于stl无序地图的任何查询,请在下面评论。

翻译自:

stl:map用法

转载地址:http://dmggb.baihongyu.com/

你可能感兴趣的文章
C#析构函数,类运行结束后运行
查看>>
在LAMP的生产环境内添加PHP的cURL扩展模块
查看>>
AMH 软件目录介绍
查看>>
你可能使用了Spring最不推荐的注解方式
查看>>
java常见3种文件上传速度对比和文件上传方法详细代码
查看>>
SVD总结
查看>>
python基础教程(三)
查看>>
PL SQL Developer中文乱码
查看>>
字符串知识大全
查看>>
软件目录结构规范及堂兄弟文件引用
查看>>
H5 WebSocket通信和WCF支持WebSocket通信
查看>>
文件上传
查看>>
不能在此路径中使用此配置节。如果在父级别上锁定了该节,便会出现这种情况...
查看>>
Linux的IO性能监控工具iostat详解
查看>>
老杨聊架构:每个架构师都应该研究下康威定律
查看>>
1022: 锤子剪刀布
查看>>
RESTful-rest_framework认证组件、权限组件、频率组件-第五篇
查看>>
手机自带功能调用
查看>>
百度搜索引擎取真实地址-python代码
查看>>
java 多线程 Future callable
查看>>