查找map中的KEY是否重复的问题!
目的:查找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() );
[解决办法]
map是一个一一对应的关系。不是一对多的。所以不能有重复的