C++98 遍历 std::map:使用迭代器访问键值对
C++98 遍历 std::map:使用迭代器访问键值对
在 C++98 标准中,您可以使用迭代器来遍历 std::map。以下是一个示例代码:
#include <iostream>
#include <map>
struct tag_ClientCBS {
// 定义 tag_ClientCBS 的成员变量和方法
};
struct CompareClientCBS {
// 定义 CompareClientCBS 的成员变量和方法
};
typedef std::map<tag_ClientCBS, int, CompareClientCBS> ClientCBSMap;
int main() {
ClientCBSMap g_clientCBS_map;
// 向 map 中插入一些数据
g_clientCBS_map[tag_ClientCBS()] = 1;
g_clientCBS_map[tag_ClientCBS()] = 2;
g_clientCBS_map[tag_ClientCBS()] = 3;
// 使用迭代器遍历 map
ClientCBSMap::iterator it;
for (it = g_clientCBS_map.begin(); it != g_clientCBS_map.end(); ++it) {
// 访问 key 和 value
tag_ClientCBS key = it->first;
int value = it->second;
// 打印 key 和 value
std::cout << 'Key: ' << key << ', Value: ' << value << std::endl;
}
return 0;
}
注意,上述示例代码中的 tag_ClientCBS 和 CompareClientCBS 是示例结构体和比较函数对象,您需要根据实际情况进行定义和实现。
原文地址: https://www.cveoy.top/t/topic/ffRD 著作权归作者所有。请勿转载和采集!