468,496 Members | 1,828 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,496 developers. It's quick & easy.

counting input words

any comments for improvement:

/* C++ Primer - 4/e
*
* CHAPTER 10 - Associative Containers
*
* EXERCISE - 10.7
* Write a program to count and print the number of times each word
* occured in the input.
*
*/
#include <iostream>
#include <string>
#include <vector>
#include <map>

int main()
{
std::map<std::string, intword_count;
std::string aword;
while( std::cin >aword )
{
++word_count[aword];
}
std::cout << "\n----------------------------------------------------\n";
for( std::map<std::string, int>::const_iterator iter =
word_count.begin();
iter != word_count.end(); ++iter )
{
std::cout << iter->first << " occured "
<< iter->second << " times "
<< std::endl;
}

return 0;
}

================= OUTPUT =======================

/home/arnuld/programs $ ./a.out
bazarov
Jack
Bux
Bux
Bazaro
Jack
Bux

----------------------------------------------------
Bazaro occured 1 times
Bux occured 3 times
Jack occured 2 times
bazarov occured 1 times
/home/arnuld/programs $

--
http://lispmachine.wordpress.com/
Nov 23 '07 #1
0 996

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by DrNoose | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.