I want to change the value of the map element if it is already exist in the map container i.e count the no of the elements in that container
std :: map < int, int > m;
std :: map < int, int > :: iterator itr;
int arr[] = { 10, 40, 20, 20, 20, 20, 20, 20, 10, 30, 10, 30, 40 };
for (int i : arr) {
itr = m.find(i);
if (itr == m.end() ) {
int value = 0;
m.insert(std :: make_pair(i, value));
} else {
++itr->second;
}
}
itr = m.begin();
while (itr != m.end() ) {
std :: cout << itr->first << " -> " << itr->second << std :: endl;
++itr;
}
I got wrong output :
10 -> 2
20 -> 5
30 -> 1
40 -> 1
Aucun commentaire:
Enregistrer un commentaire