c unordered map
时间: 2023-04-27 17:02:36 浏览: 54
unordered map是C++ STL中的一个容器,它是一个哈希表,可以用于存储键值对。它的特点是快速查找,插入和删除,时间复杂度为O(1)。与map不同的是,unordered map中的元素是无序的。
相关问题
c语言 unordered_map
在C语言中,unordered_map是一个无序容器,它可以存储键值对。你可以使用unordered_map来快速查找指定键对应的值,它的查找效率比关联式容器高。你可以使用at()函数来查找指定键对应的值。例如,在下面的代码中,我们创建了一个unordered_map容器,并初始化了一些键值对。然后使用at()函数查找指定键"C语言教程"对应的值,最后输出结果为"http://c.biancheng.net/c/"。
```c++
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
int main() {
unordered_map<string, string> my_uMap{
{"C语言教程","http://c.biancheng.net/c/"},
{"Python教程","http://c.biancheng.net/python/"},
{"Java教程","http://c.biancheng.net/java/"}
};
string str = my_uMap.at("C语言教程");
cout << "str = " << str << endl;
return 0;
}
```
此外,在C++11标准中,unordered_map模板类还增加了移动构造函数的功能。移动构造函数可以以右值引用的方式将临时unordered_map容器中存储的所有键值对复制给新建容器。例如,在下面的代码中,我们创建了一个返回临时unordered_map容器的函数retUmap(),然后使用移动构造函数创建了新的umap2容器。
```c++
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
unordered_map<string, string> retUmap(){
unordered_map<string, string> tempUmap{
{"Python教程","http://c.biancheng.net/python/"},
{"Java教程","http://c.biancheng.net/java/"},
{"Linux教程","http://c.biancheng.net/linux/"}
};
return tempUmap;
}
int main() {
unordered_map<string, string> umap2(retUmap());
return 0;
}
```
另外,你还可以使用insert()函数将一个unordered_map容器中的键值对复制给另一个unordered_map容器。下面的代码演示了如何创建一个空的unordered_map容器otherumap,并将指定unordered_map容器umap中的键值对复制给otherumap容器,然后遍历otherumap容器中的键值对并输出。
```c++
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
int main() {
unordered_map<string, string> umap{
{"STL教程","http://c.biancheng.net/stl/"},
{"Python教程","http://c.biancheng.net/python/"},
{"Java教程","http://c.biancheng.net/java/"}
};
unordered_map<string, string> otherumap;
unordered_map<string, string>::iterator first = umap.begin();
unordered_map<string, string>::iterator last = umap.end();
otherumap.insert(first, last);
for (auto iter = otherumap.begin(); iter != otherumap.end(); iter++) {
cout << iter->first << " " << iter->second << endl;
}
return 0;
}
```
这样,你就可以在C语言中使用unordered_map来存储和操作键值对了。
unordered map
unordered map是C++ STL(标准模板库)中的一个关联容器,它是一个哈希表,可以实现键值对之间的快速查找。
与其他的关联容器(如map)不同的是,unordered map并不会按照键的顺序进行排序,而是会根据键的哈希值将键值对分配到不同的桶中。这样可以实现O(1)的平均查找时间,而不受键的数量的影响。
unordered map的好处在于,它可以以常数时间进行查找,而不管元素个数的大小。另外,unordered map的添加、删除和查找操作都很快速,因此在需要进行海量数据的存储和快速查找的场合,unordered map是一个很好的选择。
但是,unordered map也有一些缺点。由于它是一个哈希表,因此它的内存开销比较大,而且在哈希冲突的情况下,排序比较困难,可能会导致性能下降。此外,每次插入、删除、查找操作时都需要进行哈希计算,这也会影响到性能表现。
总的来说,unordered map作为一个高效的关联容器,在进行数据存储和查找时有很大用处。但是,对于数据量较小的情况,可能会出现性能瓶颈。因此,在选择数据结构时,需要权衡其性能和内存开销,并根据实际场景进行选择。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)