vardgrig

Bilet3/4

Nov 30th, 2021 (edited)
233
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <fstream>
  3. #include <map>
  4. #include <vector>
  5. using namespace std;
  6.  
  7. void Print(const vector<string> &v)
  8. {
  9.     for (int i = 0; i < v.size(); ++i)
  10.         cout << v[i] << " ";
  11.     cout << endl;
  12. }
  13. int main() {
  14.     ifstream myFile;
  15.     myFile.open("file.txt");
  16.     map<int, vector<string>> m;
  17.     string s;
  18.     int line = 0;
  19.     while (!myFile.eof()) {
  20.         myFile >> s;
  21.         if (s.find('.') != string::npos)
  22.             m[line++].push_back(s);
  23.         else
  24.             m[line].push_back(s);
  25.     }
  26.     string word;
  27.     cin >> word;
  28.     int k = -1;
  29.     for (auto& i : m) {
  30.         for (int j = 0; j < i.second.size(); ++j) {
  31.             if (i.second[j] == word){
  32.                 k = i.first;
  33.                 break;
  34.             }
  35.         }
  36.     }
  37.     if (k == -1) {
  38.         cout << word << " has no synonyms in this file\n";
  39.         return 0;
  40.     }
  41.     map<int, vector<string>>::iterator it = m.find(k);
  42.     while ((it != m.end()) && ((*it).first == k)) {
  43.         Print((*it++).second);
  44.     }
  45.     return 0;
  46. }
RAW Paste Data