How to format a c++ map output appropriately? for example: map<string,int>::iter
ID: 3585275 • Letter: H
Question
How to format a c++ map output appropriately?
for example:
map<string,int>::iterator it;
for( it = wordlist.begin(); it != wordlist.end(); it++ )
cout << it->first << "(" << it->second << ")" << ", ";
has the output:
Copyright(2), Publishin(1), Universal(2), blueprint(1),
However, I want the output to be:
Copyright(2), Publishin(1), Universal(2), blueprint(1)
notice there's no ',' at the end in the desired output. Please if there is any way I can format my output to the desired output that would be great!
Explanation / Answer
Instead of putting a comma at the end, if we put it at first then it will not have a comma at the end. All we need to do is to print the first map outside the loop so that it is followed by a comma after it.
Happy Learning!
it = my_map.begin();cout << it->first << "(" << it->second << ")";
for(it++ ; it != my_map.end(); it++ )
cout << ", " << it->first << "(" << it->second << ")";
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.