cpp:stl:unordered_map
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
cpp:stl:unordered_map [2020/06/01 06:48] – gthanos | cpp:stl:unordered_map [Unknown date] (current) – external edit (Unknown date) 127.0.0.1 | ||
---|---|---|---|
Line 11: | Line 11: | ||
+ | ===== Επίδοσης της δομής ===== | ||
+ | |||
+ | Η επίδοση της δομής είναι ανάλογη των δομών [[cpp: | ||
+ | |||
+ | ===== Περισσότερα για τις κλάσεις std:: | ||
+ | |||
+ | H δήλωση της κλάσης // | ||
+ | |||
+ | <code cpp> | ||
+ | template < class Key, // unordered_map:: | ||
+ | class T, // unordered_map:: | ||
+ | class Hash = hash< | ||
+ | class Pred = equal_to< | ||
+ | class Alloc = allocator< | ||
+ | > class unordered_map; | ||
+ | </ | ||
+ | |||
+ | Από την παραπάνω δήλωση της κλάσης παρατηρούμε ότι εκτός από το κλειδί '' | ||
+ | |||
+ | Για τους βασικούς τύπους δεδομένων οι παραπάνω κλάσεις υλοποιούνται μέσω των // | ||
+ | |||
+ | |||
+ | ===== Παράδειγμα 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// από // | ||
+ | </ | ||
cpp/stl/unordered_map.txt · Last modified: 2020/06/01 06:29 (external edit)