typedef struct stdstring clientName; stdstring clientCbAddr;tag_ClientCBS; static stdmaptag_ClientCBS int CompareClientCBS g_clientCBS_map; 遍历使用c++98实现给出实现
以下是使用C++98实现遍历g_clientCBS_map的示例代码:
#include <iostream>
#include <map>
#include <string>
typedef struct
{
std::string clientName;
std::string clientCbAddr;
} tag_ClientCBS;
struct CompareClientCBS
{
bool operator()(const tag_ClientCBS& c1, const tag_ClientCBS& c2) const
{
if (c1.clientName != c2.clientName)
return c1.clientName < c2.clientName;
return c1.clientCbAddr < c2.clientCbAddr;
}
};
std::map<tag_ClientCBS, int, CompareClientCBS> g_clientCBS_map;
int main()
{
// 添加一些测试数据
tag_ClientCBS client1 = {"client1", "address1"};
tag_ClientCBS client2 = {"client2", "address2"};
tag_ClientCBS client3 = {"client3", "address3"};
g_clientCBS_map[client1] = 1;
g_clientCBS_map[client2] = 2;
g_clientCBS_map[client3] = 3;
// 遍历g_clientCBS_map
std::map<tag_ClientCBS, int, CompareClientCBS>::iterator it;
for (it = g_clientCBS_map.begin(); it != g_clientCBS_map.end(); ++it)
{
std::cout << "Client Name: " << it->first.clientName << ", ";
std::cout << "Client Cb Addr: " << it->first.clientCbAddr << ", ";
std::cout << "Value: " << it->second << std::endl;
}
return 0;
}
注意,为了使用std::map,我们需要实现一个比较函数对象CompareClientCBS,用于比较tag_ClientCBS结构体的键值。在上述示例代码中,我们使用了默认的比较函数对象,它按照clientName和clientCbAddr的字典顺序进行比较。如果你想要按照其他方式进行比较,可以根据需要修改CompareClientCBS结构体的operator()函数。
原文地址: https://www.cveoy.top/t/topic/jdmf 著作权归作者所有。请勿转载和采集!