以下是删除std::map中特定元素的实现代码:

std::map<tag_ClientCBS, int>::iterator it = g_clientCBS_map.find(tag_cbs);
if (it != g_clientCBS_map.end()) {
    g_clientCBS_map.erase(it);
}

如果要删除的元素是指针类型,则需要注意释放内存:

std::map<tag_ClientCBS, int>::iterator it = g_clientCBS_map.find(tag_cbs);
if (it != g_clientCBS_map.end()) {
    delete it->first.clientPtr;
    g_clientCBS_map.erase(it);
}

请注意,删除元素后,迭代器it将无效,不要再使用它。

stdmaptag_ClientCBS intiterator it = g_clientCBS_mapfindtag_cbs; typedef struct HelloWorldPtr clientPtr; stdstring clientName; stdstring clientCbAddr;tag_ClientCBS; 找到并删除给出实现代码

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

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