map属于什么迭代器
时间: 2023-11-27 07:55:38 浏览: 32
在C++中,`map`容器有两种迭代器:`iterator`和`const_iterator`。其中,`iterator`能够读写容器中的元素,而`const_iterator`仅能够读取容器中的元素,不能修改容器中的元素。使用方法如下:
```c++
#include <iostream>
#include <map>
int main() {
std::map<std::string, int> my_map = {{"apple", 1}, {"banana", 2}, {"orange", 3}};
// 使用 iterator 迭代器遍历 map 容器
for (std::map<std::string, int>::iterator it = my_map.begin(); it != my_map.end(); ++it) {
std::cout << it->first << " : " << it->second << std::endl;
}
// 使用 const_iterator 迭代器遍历 map 容器
for (std::map<std::string, int>::const_iterator it = my_map.cbegin(); it != my_map.cend(); ++it) {
std::cout << it->first << " : " << it->second << std::endl;
}
return 0;
}
```
输出结果:
```
apple : 1
banana : 2
orange : 3
apple : 1
banana : 2
orange : 3
```