1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
|
#include <map>
#include <iostream>
#include <string>
using namespace std;
// Insert with duplicate counting
void do_insert (map<string,int> & abc, const string & str)
{ map<string,int>::iterator iter;
pair<string,int> pr (str,1);
iter = abc.find (str);
if (iter != abc.end())
{ // str already in map
iter->second++; // Increment duplicate count
return;
}
// str not found
abc.insert (pr);
}
void report_duplicates (const map<string,int> & abc)
{ map<string,int>::const_iterator citer;
for (citer= abc.begin(); citer!=abc.end(); citer++)
{ if (citer->second > 1)
cout << citer->first << " occurred " << citer->second << " times." << endl;
}
}
int main ()
{ map<string,int> abc; // note types reversed
do_insert (abc, "i am ok");
do_insert (abc, "i am not ok");
do_insert (abc, "i am dash dash");
do_insert (abc, "i am ok"); // Duplicate
do_insert (abc, "test");
report_duplicates (abc);
system ("pause");
return 0;
}
|
i am ok occurred 2 times.
Press any key to continue . . . | |