This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision | |||
|
cpp:stl:unordered_map [2020/06/01 07:04] gthanos [Επίδοσης της δομής] |
cpp:stl:unordered_map [2020/06/01 07:29] gthanos [Περισσότερα για τις κλάσεις std::unordered_map και std::unordered_multimap] |
||
|---|---|---|---|
| Line 33: | Line 33: | ||
| + | ===== Παράδειγμα 1ο - Αποθήκευση std::string ως κλειδί σε unordered_map ή unordered_multimap ===== | ||
| + | <code cpp color_unordered_map.cpp> | ||
| + | #include <map> | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | template< | ||
| + | void print(std:: | ||
| + | for(auto it = s.cbegin(); it!=s.cend(); | ||
| + | std::cout << it-> | ||
| + | } | ||
| + | |||
| + | int main(int argc, char *argv[]) { | ||
| + | std:: | ||
| + | | ||
| + | colormap.insert(std:: | ||
| + | colormap.insert(std:: | ||
| + | colormap.insert(std:: | ||
| + | | ||
| + | colormap.insert(std:: | ||
| + | colormap.insert(std:: | ||
| + | colormap.insert(std:: | ||
| + | | ||
| + | colormap.emplace(std:: | ||
| + | colormap.emplace(std:: | ||
| + | colormap.emplace(std:: | ||
| + | | ||
| + | print(colormap); | ||
| + | | ||
| + | return 0; | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | <WRAP todo 80% center round> | ||
| + | Αλλάξτε τον //STL container// από // | ||
| + | </ | ||
| + | |||
| + | ===== Παράδειγμα 2ο - Αποθήκευση Student ως κλειδί σε unordered_map ή unordered_multimap ===== | ||
| + | |||
| + | <code cpp student_unordered_map.cpp> | ||
| + | #include < | ||
| + | #include < | ||
| + | #include " | ||
| + | |||
| + | template< | ||
| + | void print(std:: | ||
| + | for(auto it = s.cbegin(); it!=s.cend(); | ||
| + | std::cout << it-> | ||
| + | } | ||
| + | |||
| + | struct HashByAEM { | ||
| + | size_t operator()(const Student& | ||
| + | std:: | ||
| + | return hash_int(st.getAEM()); | ||
| + | } | ||
| + | }; | ||
| + | |||
| + | struct EqualToByAEM { | ||
| + | bool operator() (const Student& | ||
| + | return st1.getAEM() == st2.getAEM(); | ||
| + | } | ||
| + | }; | ||
| + | |||
| + | int main(int argc, char *argv[]) { | ||
| + | // map students to their address | ||
| + | std:: | ||
| + | | ||
| + | std:: | ||
| + | | ||
| + | std:: | ||
| + | | ||
| + | students.insert( peter_pan_pair ); | ||
| + | students.insert( mickey_mouse_pair ); | ||
| + | students.emplace( Student(" | ||
| + | | ||
| + | print(students); | ||
| + | | ||
| + | return 0; | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | <WRAP todo 80% center round> | ||
| + | Αλλάξτε τον //STL container// από // | ||
| + | </ | ||