A
arnuld
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, int> word_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 $
/* 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, int> word_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 $