Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string>
- #include <iostream>
- #include <map>
- #include <fstream>
- #include <sstream>
- using namespace std;
- int main()
- {
- ifstream fin("intrare.txt"); // fisierul de intrare
- if (!fin)
- {
- cout << "Nu se gaseste fisierul de intrare.\n";
- return -1;
- }
- ofstream fout("rezultat.txt"); // fisierul de iesire
- map<unsigned, map<string, unsigned>> M;
- unsigned i = 0; //nr de linie
- string linie;
- while (!fin.eof())
- {
- getline(fin, linie);
- stringstream ss(linie);
- string cuv;
- map<string, unsigned> aux;
- while (!ss.eof())
- {
- aux.clear();
- ss >> cuv;
- pair<map<string, unsigned>::iterator, bool> ret;
- ret = aux.insert(pair<string, unsigned>(cuv, 1));
- if (ret.second == false)
- {
- //suntem la cel putin a 2-a aparitie
- ret.first->second++;
- //ret.second este true daca exista deja cuvantul
- //ret.second este false daca nu exista cuvantul
- //ret.first este iteratorul (pointerul) catre pereche
- }
- }
- M.insert(pair<unsigned, map<string, unsigned>>(i, aux));
- i++;
- }
- map<unsigned, map<string, unsigned>>::iterator itM; //iterator pentru Map-ul mare
- map<string, unsigned>::iterator itm; //iterator pentru Map-ul mic
- for (itM = M.begin(); itM != M.end(); ++itM)
- {
- fout << "Linia " << itM->first << endl;
- for (itm = itM->second.begin(); itm != itM->second.end(); ++itm)
- {
- fout << itm->first << " " << itm->second;
- }
- }
- fin.close();
- fout.close();
- cout << "Gata. Vezi rezultatul in fisier.";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment