shushus

Спринт 2. Вычисляем term frequencies.

Dec 8th, 2022
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.64 KB | None | 0 0
  1. #include <iostream>
  2. #include <map>
  3. #include <string>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. map<string, int> ComputeTermFreqs(const vector<string>& terms) {
  9.     // реализуйте функцию
  10.     map<string, int> result;
  11.     for (const string& word : terms) {
  12.         int counter = 1;
  13.         result[word]+=counter;
  14.     }
  15.     return result;
  16. }
  17.  
  18. int main() {
  19.     const vector<string> terms = {"first"s, "time"s, "first"s, "class"s};
  20.     for (const auto& [term, freq] : ComputeTermFreqs(terms)) {
  21.         cout << term << " x "s << freq << endl;
  22.     }
  23.     // вывод:
  24.     // class x 1
  25.     // first x 2
  26.     // time x 1
  27. }
Advertisement
Add Comment
Please, Sign In to add comment