Advertisement
chevengur

СПРИНТ № 2 | Шаблоны функции | Урок 2: Вычисляем term frequencies

Oct 24th, 2023 (edited)
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.57 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.     map<string, int>freq_doc;
  10.  
  11.     for(const auto& term: terms){
  12.         freq_doc[term]++;
  13.     }
  14.     return freq_doc;
  15. }
  16.  
  17. int main() {
  18.     const vector<string> terms = {"first"s, "time"s, "first"s, "class"s};
  19.     for (const auto& [term, freq] : ComputeTermFreqs(terms)) {
  20.         cout << term << " x "s << freq << endl;
  21.     }
  22.     // вывод:
  23.     // class x 1
  24.     // first x 2
  25.     // time x 1
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement