std::map<std::string, std::string> 遍历
时间: 2023-09-10 19:11:18 浏览: 90
Map遍历方法
可以使用迭代器来遍历 std::map<std::string, std::string>,示例代码如下:
```cpp
#include <iostream>
#include <map>
#include <string>
int main() {
std::map<std::string, std::string> myMap;
myMap["apple"] = "red";
myMap["banana"] = "yellow";
myMap["grape"] = "purple";
// 使用迭代器遍历 map
std::map<std::string, std::string>::iterator it;
for (it = myMap.begin(); it != myMap.end(); it++) {
std::cout << it->first << " is " << it->second << std::endl;
}
return 0;
}
```
输出结果:
```
apple is red
banana is yellow
grape is purple
```
阅读全文