写一个C++ 程序,如何判断对于map中不同key,是否有重复的value?

2025-01-05 22:38:36
推荐回答(3个)
回答1:

就顺序读取map中的每个key,当遇到一个key都放到一个数组中标记次数,然后看有没有大于1的,就知道是否有重复的value了

回答2:

for(map::iterator it = g_map.begin(); it != g_map.end(); it++)
{
for(map::iterator it1 = g_map.begin();it1 != g_map.end(); it1++)
{
if(it->first != it1->first && it->second == it1->second)

return 1;
}
}
return 0;

回答3:

typedef map mapConnectManager;
mapConnectManager m_mapConnectManager;
int count = 0;
for (mapConnectManager::iterator it = m_mapConnectManager.begin(); it != m_mapConnectManager.end(); ++it)
{ // store element in key_type object
mapConnectManager::key_type val = it->second->GetDeviceID();
for (mapConnectManager::iterator it2 = m_mapConnectManager.begin(); it2 != m_mapConnectManager.end(); ++it2)
{
mapConnectManager::key_type val2 = it->second->GetDeviceID();
if (val == val2) count++;
if (count >= 2) return;
}

}