I have a code in c++ with stl map with the second parameter defined as a pair
int keys[10] = {1, 1, 1, 2, 3, 4, 5, 7, 6, 6};
char s[5];
map< unsigned int, pair<string, int> > tmpMap;
for (int i=0; i<10; i++)
{
if (tmpMap.find(keys[i])==tmpMap.end())
{
sprintf(s, "%i", keys[i]);
tmpMap.insert(make_pair(keys[i], make_pair(s, 1)));
}
else tmpMap[keys[i]].second++;
}
for (map< unsigned int, pair<string, int> >::iterator it=tmpMap.begin(); it!=tmpMap.end(); ++it)
{
cout << (*it).first << " " << (*it).second << endl;
}
But it fail to compile, it said, no match operator <<. But (*it).first and (*it).second are just string and int, why it doesn’t work?
That’s not true,
firstis anunsigned int, whilesecondis apair<string,int>because the iterator of a map doesn’t give you directly the pair but the couple key,value.I guess you should do