Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<string>
- #include<algorithm>
- #include<unordered_map>
- #include<iostream>
- using namespace std;
- class myClass { // Clasa cu nume si prenume hash_mapul va contine myClass ca si cheie si un int ca si value
- string nume, prenume;
- public: myClass(string A = "John", string B = "Doe") // construcctor cu param impliciti
- {
- nume = B; prenume = A;
- }
- string get_Nume() { return prenume + " " + nume; } // daca am nevoie de un return pt valoare...
- bool operator ==(const myClass Ob) const // clase are nevoie de operatorul ==
- { return nume == Ob.nume && prenume == Ob.prenume; } // De ce? Nu stiu...
- friend ostream& operator << (ostream&, myClass); // pt afisare directa a unui element de tip myClass cu cout
- friend struct mySimpleHash;
- friend struct myProfiHash;
- };
- ostream& operator <<(ostream &O, myClass X)
- {
- O<< X.get_Nume();
- return O;
- }
- struct myStupidHash { //structura
- size_t operator()(const myClass &Ob) const //supraincarca operatorul ()
- {
- const size_t unu = 1;
- return unu; // returneaza un intreg pe post de hash
- }
- };
- struct mySimpleHash {
- size_t operator()(const myClass &Ob)const
- {
- return hash<string>()(Ob.nume) + hash<string>()(Ob.prenume);
- // return hash_value(Ob.nume)+hash_value(Ob.prenume);
- }
- };
- struct myProfiHash
- // hash_combine, hash_value - library de pe http://www.boost.org/
- {
- size_t operator()(const myClass&Ob) const
- {
- size_t rez = 17;
- rez = rez * 31 + hash<string>()(Ob.nume);
- rez = rez * 31 + hash<string>()(Ob.prenume);
- return rez;
- }
- };
- int main() {
- unordered_map<myClass, int,myStupidHash> myMap{ {{"Ion","Vasile"},15},{ { "Vasile","Ion" },51 },{ { "Stefan","Popescu" },33 } };
- myMap.insert({ {"Pavel","Nedved"},43 });
- for (auto i = myMap.begin(); i != myMap.end();++i) {
- cout << i->first << " " << i->second<<'\n';
- }
- auto it = myMap.find({ "Stefan","Popescu" }); //it - un iterator la o lista; mai simplu sa il declar auto
- if (it == myMap.end()) //Atentie daca nu gaseste 'it' nu devine NULL ci map::end() !!!!
- cout << "Nu am gasit" << endl;
- else
- cout << it->first << " " << it->second << "\n";
- it = myMap.find({ "Popescu","Stefan" });
- if (it == myMap.end())
- cout << "Nu am gasit" << endl;
- else
- cout << it->first << " " << it->second << "\n";
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement