查找map中的KEY是否重复的问题!

wangsiyuanoo wangsiyuanoo 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
目的:查找m_mapNewWangBaInfo中key位置保存的IP是否存在重复的现象。
我的方法如下,有点笨,但只求问对不对?
因为map会自动排序key,所以我拿第一个和第二个比,觉得就会找出是否重复了。


m_vecRepeatAllIP.clear();
string strIP1 = "";
string strIP2 = "";
map<string, stWangBaInfo*>::iterator itNew = m_mapNewWangBaInfo.begin();
for ( ; itNew != m_mapNewWangBaInfo.end(); ++itNew )
{
if ( itNew == (--m_mapNewWangBaInfo.end()) )
break;

strIP1 = itNew->first;

++itNew;
strIP2 = itNew->first;
if ( 0 == strcmp( strIP1.c_str(), strIP2.c_str() ) )
{
m_vecRepeatAllIP.push_back( strIP1 );
}

--itNew;
}
sort( m_vecRepeatAllIP.begin(), m_vecRepeatAllIP.end() );
vector<string>::iterator it = unique( m_vecRepeatAllIP.begin(), m_vecRepeatAllIP.end() );
m_vecRepeatAllIP.erase( it, m_vecRepeatAllIP.end() );
...全文

相关推荐

评论 15
收藏 已收藏
分享
返回顶部
wangsiyuanoo
C++ 语言 65,214 成员 250,514 内容