ToniDev

lab8/ex.3

Jan 4th, 2022
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.54 KB | None | 0 0
  1. #include <string>
  2. #include <iostream>
  3. #include <map>
  4. #include <fstream>
  5. #include <sstream>
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10.     ifstream fin("intrare.txt"); // fisierul de intrare
  11.     if (!fin)
  12.     {
  13.         cout << "Nu se gaseste fisierul de intrare.\n";
  14.         return -1;
  15.     }
  16.  
  17.     ofstream fout("rezultat.txt"); // fisierul de iesire
  18.  
  19.  
  20.     map<unsigned, map<string, unsigned>> M;
  21.  
  22.    
  23.  
  24.  
  25.     unsigned i = 0; //nr de linie
  26.  
  27.     string linie;
  28.     while (!fin.eof())
  29.     {
  30.         getline(fin, linie);
  31.         stringstream ss(linie);
  32.  
  33.         string cuv;
  34.         map<string, unsigned> aux;
  35.  
  36.         while (!ss.eof())
  37.         {
  38.             aux.clear();
  39.  
  40.             ss >> cuv;
  41.  
  42.             pair<map<string, unsigned>::iterator, bool> ret;
  43.  
  44.             ret = aux.insert(pair<string, unsigned>(cuv, 1));
  45.  
  46.             if (ret.second == false)
  47.             {
  48.                 //suntem la cel putin a 2-a aparitie
  49.                 ret.first->second++;
  50.  
  51.                 //ret.second este true daca exista deja cuvantul
  52.                 //ret.second este false daca nu exista cuvantul
  53.                 //ret.first este iteratorul (pointerul) catre pereche
  54.             }
  55.         }
  56.  
  57.         M.insert(pair<unsigned, map<string, unsigned>>(i, aux));
  58.         i++;
  59.     }
  60.  
  61.     map<unsigned, map<string, unsigned>>::iterator itM; //iterator pentru Map-ul mare
  62.     map<string, unsigned>::iterator itm; //iterator pentru Map-ul mic
  63.  
  64.     for (itM = M.begin(); itM != M.end(); ++itM)
  65.     {
  66.         fout << "Linia " << itM->first << endl;
  67.         for (itm = itM->second.begin(); itm != itM->second.end(); ++itm)
  68.         {
  69.             fout << itm->first << " " << itm->second;
  70.         }
  71.     }
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.     fin.close();
  79.     fout.close();
  80.  
  81.     cout << "Gata. Vezi rezultatul in fisier.";
  82.  
  83.     return 0;
  84. }
Advertisement
Add Comment
Please, Sign In to add comment