以下是使用C++98实现的代码:

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

struct CompareClientCBS
{
    bool operator()(const tag_ClientCBS& c1, const tag_ClientCBS& c2) const
    {
        return c1.clientCbAddr < c2.clientCbAddr;
    }
};

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

struct CompareClientCBS
{
    bool operator()(const tag_ClientCBS& c1, const tag_ClientCBS& c2) const
    {
        return c1.clientCbAddr < c2.clientCbAddr;
    }
};

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

int main()
{
    tag_ClientCBS client1;
    client1.clientName = "Client1";
    client1.clientCbAddr = "Addr1";
    g_clientCBS_map[client1] = 1;

    tag_ClientCBS client2;
    client2.clientName = "Client2";
    client2.clientCbAddr = "Addr2";
    g_clientCBS_map[client2] = 2;

    tag_ClientCBS client3;
    client3.clientName = "Client3";
    client3.clientCbAddr = "Addr1"; // Same clientCbAddr as client1
    g_clientCBS_map[client3] = 3;

    std::cout << "Size of map: " << g_clientCBS_map.size() << std::endl;

    return 0;
}

这段代码定义了一个tag_ClientCBS结构体,其中包含两个字符串成员clientNameclientCbAddr。然后定义了一个CompareClientCBS结构体,用于比较tag_ClientCBS结构体对象的clientCbAddr成员。接下来定义了一个std::map对象g_clientCBS_map,使用tag_ClientCBS作为键,整数作为值,并使用CompareClientCBS作为比较函数。

main函数中,创建了三个tag_ClientCBS对象,并将它们插入到g_clientCBS_map中。最后输出了g_clientCBS_map的大小。

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/jdn2 著作权归作者所有。请勿转载和采集!

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