c++ map有几种类型
时间: 2023-09-21 22:12:59 浏览: 59
在 C++ 中,std::map 是一个关联容器,用于存储键值对,其中键和值可以是任何类型。在 C++ 中,std::map 实际上只有一种类型,即 std::map<Key, T>,其中 Key 是键的类型,T 是值的类型。同时,C++11 引入了一种无序关联容器 std::unordered_map,也只有一种类型,即 std::unordered_map<Key, T>。这两种类型实现了不同的底层数据结构,std::map 基于红黑树,std::unordered_map 基于哈希表。
相关问题
C++ qt Map的几种写法
在C++中,可以使用Qt提供的Map容器类来实现。以下是几种常见的写法:
1. QMap:
```
QMap<QString, int> map;
map.insert("apple", 1);
map.insert("banana", 2);
map.insert("orange", 3);
int value = map.value("apple"); // 1
```
2. QHash:
```
QHash<QString, int> hash;
hash.insert("apple", 1);
hash.insert("banana", 2);
hash.insert("orange", 3);
int value = hash.value("apple"); // 1
```
3. QMultiMap:
```
QMultiMap<QString, int> map;
map.insert("apple", 1);
map.insert("apple", 2);
map.insert("orange", 3);
QList<int> values = map.values("apple"); // [1, 2]
```
4. QMultiHash:
```
QMultiHash<QString, int> hash;
hash.insert("apple", 1);
hash.insert("apple", 2);
hash.insert("orange", 3);
QList<int> values = hash.values("apple"); // [1, 2]
```
5. QSharedPointer<QMap>:
```
QSharedPointer<QMap<QString, int>> map = QSharedPointer<QMap<QString, int>>::create();
map->insert("apple", 1);
map->insert("banana", 2);
map->insert("orange", 3);
int value = map->value("apple"); // 1
```
这些容器类的使用方式和STL中的容器类类似,也可以使用迭代器进行遍历、查找等操作。
c++ map 遍历
在C++中,可以使用迭代器、range for循环和C++17的结构化绑定来遍历map。
一种遍历方式是使用迭代器。可以通过使用map的begin()和end()函数来获取map的起始和结束迭代器,然后使用一个循环来迭代访问每个元素。例如,可以使用以下代码进行遍历:
#include <map>
#include <iostream>
int main() {
std::map<int, std::string> mapStudent;
mapStudent = "student_one";
mapStudent = "student_two";
mapStudent = "student_three";
std::map<int, std::string>::iterator iter; // 声明迭代器
for (iter = mapStudent.begin(); iter != mapStudent.end(); iter++) {
std::cout << iter->first << " " << iter->second << std::endl;
}
return 0;
}
另一种遍历方式是使用C++11及以上版本的range for循环。可以直接使用auto关键字来声明一个变量来表示每个键值对,并通过range for循环遍历map。以下是示例代码:
#include <iostream>
#include <unordered_map>
int main() {
std::unordered_map<std::string, int> mp;
mp["张三"] = 20;
mp["李四"] = 18;
mp["王五"] = 30;
std::cout << "方式一、迭代器" << std::endl;
for (auto it = mp.begin(); it != mp.end(); it++) {
std::cout << it->first << " " << it->second << std::endl;
}
std::cout << "\n方法二、range for" << std::endl;
for (auto it : mp) {
std::cout << it.first << " " << it.second << std::endl;
}
std::cout << "\n方法三" << std::endl;
for (auto [key, val] : mp) {
std::cout << key << " " << val << std::endl;
}
return 0;
}
另外还可以使用简单的for循环和size()函数来遍历map。以下是示例代码:
#include <map>
#include <iostream>
int main() {
std::map<int, std::string> mapStudent;
mapStudent = "student_one";
mapStudent = "student_two";
mapStudent = "student_three";
int iSize = mapStudent.size();
for (int i = 1; i <= iSize; i++) {
std::cout << i << " " << mapStudent[i] << std::endl;
}
return 0;
}
以上是C++中遍历map的几种方式。可以根据实际情况选择合适的方式来遍历map。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *3* [C++中map的遍历](https://blog.csdn.net/chengqiuming/article/details/89815814)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
- *2* [C++ map遍历的几种方式](https://blog.csdn.net/VariatioZbw/article/details/124947520)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)