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 07:04] – [Επίδοσης της δομής] gthanos | cpp:stl:unordered_map [Unknown date] (current) – external edit (Unknown date) 127.0.0.1 | ||
|---|---|---|---|
| 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// από // | ||
| + | </ | ||
cpp/stl/unordered_map.1590995063.txt.gz · Last modified: 2020/06/01 06:04 (external edit)
