遍歷容器,找到數值最大的int值,然後再次遍歷,判斷相等就返回所需的string.
下面是代碼:
int nMax = 0;
for(map<string,int>::iterator iter = clor.begin() ; iter != ?clor.end() ; ++iter)
{
int n = iter->second;
if( n > nMax )
nMax = n;
}
string stValue;
for(map<string,int>::iterator iter = clor.begin() ; iter != ?clor.end() ; ++iter)
{
if(iter->second == nMax)
{
strValue = iter->first;
break;
}
}
容器map的遍歷方法:
1、常規方法
2、利用keyset進行遍歷,其優點在於可以根據所想要的key值得到想要的 values,更具靈活性。
3、比較復雜的壹種遍歷在這裏,其靈活性極強,想得到什麽就能得到什麽。