User Tools

Site Tools


cpp:stl:unordered_map

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
cpp:stl:unordered_map [2020/06/01 07:04] – [Επίδοσης της δομής] gthanoscpp: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 <utility>
 +#include <string>
 +#include <iostream>
 +
 +template<typename K, typename V, typename Hash, typename Pred>
 +void print(std::map<K,V,Hash,Pred> s) {
 +  for(auto it = s.cbegin(); it!=s.cend(); it++) 
 +    std::cout << it->first << " -> " << std::hex << std::uppercase << "0x" << it->second << std::endl;
 +}
 +
 +int main(int argc, char *argv[]) {
 +  std::map<std::string, int> colormap;
 +  
 +  colormap.insert(std::pair<std::string, int>(std::string("orange"), 0xFF8C00 ));
 +  colormap.insert(std::pair<std::string, int>(std::string("green"), 0x008C00 ));
 +  colormap.insert(std::pair<std::string, int>(std::string("blue"), 0x0000FF ));
 +  
 +  colormap.insert(std::make_pair(std::string("red"), 0x8C0000 ));
 +  colormap.insert(std::make_pair(std::string("cyan"), 0x00FFFF));
 +  colormap.insert(std::make_pair(std::string("orange"), 0xFF6347 ));
 +  
 +  colormap.emplace(std::string("blue"), 0x0000AA );
 +  colormap.emplace(std::string("purple"), 0x9400D3 );
 +  colormap.emplace(std::string("magenta"), 0xFF00FF );
 +  
 +  print(colormap);
 +  
 +  return 0;
 +}
 +</code>
 +
 +<WRAP todo 80% center round>
 +Αλλάξτε τον //STL container// από //std::unordered_map// σε //std::unordered_multimap// και παρατηρήστε την συμπεριφορά του προγράμματος σε αυτή την περίπτωση.
 +</WRAP>
 +
 +===== Παράδειγμα 2ο - Αποθήκευση Student ως κλειδί σε unordered_map ή unordered_multimap =====
 +
 +<code cpp student_unordered_map.cpp>
 +#include <unordered_map>
 +#include <iostream>
 +#include "Student.hpp"
 +
 +template<typename K, typename V, typename Hash, typename Pred>
 +void print(std::unordered_map<K,V,Hash,Pred> s) {
 +  for(auto it = s.cbegin(); it!=s.cend(); it++) 
 +    std::cout << it->first << " -> " << it->second << std::endl;
 +}
 +
 +struct HashByAEM {
 +  size_t operator()(const Student& st) const {
 +    std::hash<int> hash_int;
 +    return hash_int(st.getAEM());
 +  }
 +};
 +
 +struct EqualToByAEM {
 +  bool operator() (const Student& st1, const Student& st2) const {
 +    return st1.getAEM() == st2.getAEM();
 +  }
 +};
 +
 +int main(int argc, char *argv[]) {
 +  // map students to their address
 +  std::unordered_map<Student, std::string, HashByAEM, EqualToByAEM> students;
 +  
 +  std::pair<Student,std::string> peter_pan_pair(Student("Peter Pan", 1234), std::string("Wendy House, Neverland"));
 +  
 +  std::pair<Student,std::string> mickey_mouse_pair = make_pair(Student("Mickey Mouse", 1235), std::string("Walt Disney World Communications, P.O Box 10040, Lake Buena Vista, Florida 32830-0040 "));
 +  
 +  students.insert( peter_pan_pair );
 +  students.insert( mickey_mouse_pair );
 +  students.emplace( Student("Minie Mouse", 1240), std::string("Walt Disney World Communications, P.O Box 10040, Lake Buena Vista, Florida 32830-0040 "));
 +  
 +  print(students);
 +  
 +  return 0;
 +}
 +</code>
 +
 +<WRAP todo 80% center round>
 +Αλλάξτε τον //STL container// από //std::unordered_set// σε //std::unordered_multiset// και παρατηρήστε τη συμπεριφορά του προγράμματος και σε αυτή την περίπτωση.
 +</WRAP>
  
  
cpp/stl/unordered_map.1590995063.txt.gz · Last modified: 2020/06/01 06:04 (external edit)