Advertisement
StefanPopescu

Untitled

Sep 6th, 2017
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.33 KB | None | 0 0
  1. #include<string>
  2. #include<algorithm>
  3. #include<unordered_map>
  4. #include<iostream>
  5.  
  6. using namespace std;
  7. class myClass {   // Clasa cu nume si prenume hash_mapul va contine myClass ca si cheie si un int ca si value
  8.     string nume, prenume;
  9. public: myClass(string A = "John", string B = "Doe") // construcctor cu param impliciti
  10.     {
  11.     nume = B; prenume = A;
  12.     }
  13.     string get_Nume() { return prenume + " " + nume; } // daca am nevoie de un return pt valoare...
  14.  
  15.     bool operator ==(const myClass Ob) const // clase are nevoie de operatorul ==
  16.     { return nume == Ob.nume && prenume == Ob.prenume; } // De ce? Nu stiu...
  17.  
  18.     friend ostream& operator << (ostream&, myClass); // pt afisare directa a unui element de tip myClass cu cout
  19.     friend struct mySimpleHash;
  20.     friend struct myProfiHash;
  21. };
  22.  
  23. ostream& operator <<(ostream &O, myClass X)
  24. {
  25.    
  26.      O<< X.get_Nume();
  27.      return O;
  28.  
  29.     }
  30.  
  31. struct  myStupidHash { //structura
  32.     size_t operator()(const myClass &Ob) const //supraincarca operatorul ()
  33.     {
  34.         const size_t unu = 1;
  35.         return unu; // returneaza un intreg pe post de hash
  36.     }
  37.  
  38.  
  39. };
  40.  
  41. struct  mySimpleHash {
  42.     size_t operator()(const myClass &Ob)const
  43.     {
  44.         return hash<string>()(Ob.nume) + hash<string>()(Ob.prenume);
  45.         // return hash_value(Ob.nume)+hash_value(Ob.prenume);
  46.     }
  47.    
  48.  
  49. };
  50.  
  51.  
  52. struct myProfiHash
  53.     // hash_combine, hash_value - library de pe http://www.boost.org/
  54. {
  55.     size_t operator()(const myClass&Ob) const
  56.     {
  57.        
  58.         size_t rez = 17;
  59.             rez = rez * 31 + hash<string>()(Ob.nume);
  60.             rez = rez * 31 + hash<string>()(Ob.prenume);
  61.             return rez;
  62.  
  63.     }
  64.  
  65. };
  66.  
  67.  
  68. int main() {
  69.  
  70.     unordered_map<myClass, int,myStupidHash> myMap{ {{"Ion","Vasile"},15},{ { "Vasile","Ion" },51 },{ { "Stefan","Popescu" },33 } };
  71.     myMap.insert({ {"Pavel","Nedved"},43 });
  72.     for (auto i = myMap.begin(); i != myMap.end();++i) {
  73.         cout << i->first << " " << i->second<<'\n';
  74.     }
  75.    
  76.  
  77.     auto it = myMap.find({ "Stefan","Popescu" }); //it - un iterator la o lista; mai simplu sa il declar auto
  78.     if (it == myMap.end()) //Atentie daca nu gaseste 'it' nu devine NULL ci map::end() !!!!
  79.         cout << "Nu am gasit" << endl;
  80.     else
  81.         cout << it->first << " " << it->second << "\n";
  82.  
  83.     it = myMap.find({ "Popescu","Stefan" });
  84.     if (it == myMap.end())
  85.         cout << "Nu am gasit" << endl;
  86.     else
  87.         cout << it->first << " " << it->second << "\n";
  88.  
  89.     system("pause");
  90.     return 0;
  91.  
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement