Advertisement
Guest User

Untitled

a guest
Jul 7th, 2017
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.10 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9.     vector<string> words;
  10.     string word;
  11.  
  12.     while (cin >> word){
  13.         if (word == "q"){
  14.             break;
  15.         }
  16.         words.push_back(word);
  17.     }
  18.  
  19.     typedef vector<double>::size_type vec_sz;
  20.     vec_sz sizeOwords = words.size();
  21.  
  22.     sort(words.begin(), words.end());
  23.  
  24.     int wordCount = 1;
  25.  
  26.     for (int i = 0; i != sizeOwords; i++){
  27.         // make sure we're in bounds of the vector
  28.         if (i + 1 < sizeOwords){
  29.             // increment wordCount
  30.             if (words[i] == words[i + 1]){
  31.                 wordCount++;
  32.             } else {
  33.                 // once two words no longer match, we spit out the counts
  34.                 // and reset wordcount
  35.                 cout << words[i] << " count: " << wordCount << endl;
  36.                 wordCount = 1;
  37.             }
  38.         } else {
  39.             // same as above, but for the last element of the vector
  40.             cout << words[i] << " count: " << wordCount << endl;
  41.             wordCount = 1;
  42.         }
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement