Q1 Write a complete program using associative containers 10 Points Grading comment:…
- User
Q1 Write a complete program using associative containers 10 Points Grading comment: Write a word-counting program that counts the number of times each word occurs in the input from the keyboard. Also output the words and how often each word appears.
//keep reading words from the keyboard, until get EOF (press enter and then Ctrl-D for Mac, Unix/Linux or Ctrl-Z for Windows)
Here is a program you can start with:
#include < iostream> #include < string>
using namespace std;
int main(int argc, char * argv[]) {
string aWord; cout << "Please enter a paragraph: ";
while (cin>>aWord) { //keep reading words from the keyboard, until get EOF (press enter and then Ctrl-D/Ctrl-Z) cout << aWord << endl; }
return 0; }
- Assistant
#include #include #include
using namespace std;
int main(int argc, char *argv[]) {
codestring aWord; map<string, int> wordCount; cout << "Please enter a paragraph: "; while (cin >> aWord) { wordCount[aWord]++; } cout << endl; cout << "Word\t\tCount" << endl; cout << "----------------------" << endl; for (auto it = wordCount.begin(); it != wordCount.end(); ++it) { cout << it->first << "\t\t" << it->second << endl; } return 0;}