以下是使用C++98实现的遍历和插入函数:

#include <iostream>
#include <map>
#include <string>

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;
    }
};

typedef struct 
{
    std::string clientName;
    std::string clientCbAddr;
} tag_ClientCBS;

static std::map<tag_ClientCBS, int, CompareClientCBS> g_clientCBS_map;

void InsertClientCBS(const std::string& clientName, const std::string& clientCbAddr)
{
    tag_ClientCBS clientCBS;
    clientCBS.clientName = clientName;
    clientCBS.clientCbAddr = clientCbAddr;
    
    g_clientCBS_map[clientCBS] = 0;
}

void TraverseClientCBS()
{
    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 Callback Address: " << it->first.clientCbAddr << std::endl;
    }
}

int main()
{
    InsertClientCBS("Client1", "Address1");
    InsertClientCBS("Client2", "Address2");
    InsertClientCBS("Client3", "Address3");
    
    TraverseClientCBS();
    
    return 0;
}

这段代码定义了一个tag_ClientCBS结构体,包含了客户端名称和回调地址两个成员变量。CompareClientCBS结构体定义了一个比较函数,用于在std::map中对tag_ClientCBS进行排序和比较。g_clientCBS_map是一个std::map对象,以tag_ClientCBS作为键,整数作为值。

InsertClientCBS函数用于插入一个新的tag_ClientCBS对象到g_clientCBS_map中,其中传入的clientNameclientCbAddr参数用于初始化tag_ClientCBS对象。

TraverseClientCBS函数用于遍历g_clientCBS_map,并打印出每个tag_ClientCBS对象的客户端名称和回调地址。

main函数中,我们插入了三个tag_ClientCBS对象,并调用TraverseClientCBS函数进行遍历和打印输出。

请注意,由于C++98不支持std::string的字面值初始化,因此在实际使用时,可能需要将字符串字面值转换为std::string对象。

typedef struct stdstring clientName; stdstring clientCbAddr;tag_ClientCBS; static stdmaptag_ClientCBS int CompareClientCBS g_clientCBS_map; 遍历 插入只管clientCbAddr使用c++98实现给出实现

原文地址: https://www.cveoy.top/t/topic/jdn0 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录