User Tools

Site Tools


cpp:stl: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:map [2020/05/31 08:25] – [H συνάρτηση operator[]] gthanoscpp:stl:map [Unknown date] (current) – external edit (Unknown date) 127.0.0.1
Line 5: Line 5:
 Τα στοιχεία-κλειδιά αποθηκεύονται εσωτερικά σε ένα ισοζυγισμένο δέντρο αναζητήσεως (π.χ.[[wp>Red–black_tree]], [[wp>AVL_tree]]), όπως ακριβώς και στο [[cpp:stl:set|std::set]]. Τα στοιχεία-κλειδιά αποθηκεύονται εσωτερικά σε ένα ισοζυγισμένο δέντρο αναζητήσεως (π.χ.[[wp>Red–black_tree]], [[wp>AVL_tree]]), όπως ακριβώς και στο [[cpp:stl:set|std::set]].
  
-Η κατάταξη ενός νέου στοιχείου-κλειδιού γίνεται πάντα μέσω σύγκρισης με τα υπόλοιπα στοιχεία που είναι αποθηκευμένα στην δομή. Για τον λόγο αυτό, απαραίτητη προϋπόθεση για τη λειτουργία του //map// (όπως και στο [[cpp:stl:set|std::set]]) είναι για τον τύπο του κλειδιού να παρέχονται οι τελεστές σύγκρισης %%<%% και %%>%%. Δύο κλειδιά ιδίου τύπου θεωρούνται ίσα σε ένα //map// εάν η παρακάτω λογική έκφραση είναι αληθής.+Η κατάταξη ενός νέου στοιχείου-κλειδιού γίνεται πάντα μέσω σύγκρισης με τα υπόλοιπα στοιχεία που είναι αποθηκευμένα στην δομή. Για τον λόγο αυτό, απαραίτητη προϋπόθεση για τη λειτουργία του //map// (όπως και στο [[cpp:stl:set|std::set]]) είναι για τον τύπο του κλειδιού να παρέχονται ο τελεστής σύγκρισης %%<%%. Δύο κλειδιά ιδίου τύπου θεωρούνται ίσα σε ένα //map// εάν η παρακάτω λογική έκφραση είναι αληθής.
  
 <code cpp> <code cpp>
Line 64: Line 64:
 void print(std::map<K,V> s) { void print(std::map<K,V> s) {
   for(auto it = s.cbegin(); it!=s.cend(); it++)    for(auto it = s.cbegin(); it!=s.cend(); it++) 
-    std::cout << it->first << " -> " << it->second << std::endl;+    std::cout << it->first << " -> " << std::hex << std::uppercase << "0x" << it->second << std::endl;
 } }
  
Line 124: Line 124:
   std::string address_mm = students[ Student("Minie Mouse", 1240) ];   // address_mm does not exist   std::string address_mm = students[ Student("Minie Mouse", 1240) ];   // address_mm does not exist
      
-  std::cout << "Peter Pan aem: " << aem_pp << std::endl; +  std::cout << "Peter Pan   address: " << address_pp << std::endl; 
-  std::cout << "Minie Mouse aem: " << aem_mm << std::endl;             // empty string+  std::cout << "Minie Mouse address: " << address_mm << std::endl;             // empty string
 } }
 </code> </code>
Line 160: Line 160:
 } }
 </code> </code>
 +
 +
 +==== H συνάρτηση at ====
 +
 +Η συνάρτηση [[http://www.cplusplus.com/reference/map/map/at| at ]] έχει ανάλογο ρόλο με τη συνάρτηση [[http://www.cplusplus.com/reference/map/map/operator[]| operator[] ]], δηλαδή μπορεί να διαβάσει ή να θέσει την τιμή ενός κλειδιού στο //std::map//. Η διαφορά της συγκεκριμένης συνάρτησης σε σχέση με την [[http://www.cplusplus.com/reference/map/map/operator[]| operator[] ]] είναι ότι εάν το κλειδί δεν υπάρχει στο //std::map// παράγει ένα //exception// του τύπου [[http://www.cplusplus.com/reference/stdexcept/out_of_range/|std::out_of_range]] .
 +
 +=== Παράδειγμα 1ο - Ανάγνωση με χρήση της συνάρτησης at ===
 +
 +<code cpp student_map_read_using_at.cpp>
 +#include <map>
 +#include <string>
 +#include <utility>
 +#include <iostream>
 +
 +#include "Student.hpp"
 +
 +int main() {
 +  std::pair<Student,std::string> peter_pan(Student("Peter Pan", 1234), std::string("Wendy House, Neverland"));
 +  
 +  std::pair<Student,std::string> mickey_mouse = make_pair(Student("Mickey Mouse", 1235), std::string("Walt Disney World Communications, P.O Box 10040, Lake Buena Vista, Florida 32830-0040 "));
 +  
 +  std::map<Student, std::string> students;
 +  students.insert( peter_pan );
 +  students.insert( mickey_mouse );
 +  
 +  std::string address_pp;
 +  std::string address_mm;
 +  
 +  try {
 +    address_pp = students.at(Student("Peter Pan", 1234));     // address_pp exists
 +  }
 +  catch(std::out_of_range& ex) {
 +    std::cout << "Student '" << Student("Peter Pan", 1234) << "' does not exist in map\n\n";
 +  }
 +  
 +  try {
 +    address_mm = students.at(Student("Minie Mouse", 1240));   // address_mm does not exist
 +  }
 +  catch(std::out_of_range& ex) {
 +    std::cout << "Student '" << Student("Minie Mouse", 1240) << "' does not exist in map\n\n";
 +  }
 +  
 +  std::cout << "Peter Pan address: " << address_pp << std::endl;
 +  std::cout << "Minie Mouse address: " << address_mm << std::endl;             // empty string
 +}
 +
 +</code>
 +
 +=== Παράδειγμα 2ο - Εγγραφή με χρήση της συνάρτησης at ===
 +
 +<code cpp student_map_write_using_at.cpp>
 +#include <map>
 +#include <string>
 +#include <utility>
 +#include <iostream>
 +
 +#include "Student.hpp"
 +
 +template<typename K, typename V>
 +void print(std::map<K,V> s) {
 +  for(auto it = s.cbegin(); it!=s.cend(); it++) 
 +    std::cout << it->first << " -> " << it->second << std::endl;
 +}
 +
 +int main() {
 +  std::pair<Student,std::string> peter_pan(Student("Peter Pan", 1234), std::string("Wendy House, Neverland"));
 +  
 +  std::pair<Student,std::string> mickey_mouse = make_pair(Student("Mickey Mouse", 1235), std::string("Walt Disney World Communications, P.O Box 10040, Lake Buena Vista, Florida 32830-0040 "));
 +  
 +  std::map<Student, std::string> students;
 +  students.insert( peter_pan );
 +  students.insert( mickey_mouse );
 +  
 +  try {
 +    students.at( Student("Peter Pan", 1234) ) = std::string("Home Under The Ground, Neverland");
 +  }
 +  catch(std::out_of_range& ex) {
 +    std::cout << "Student '" << Student("Peter Pan", 1234) << "' does not exist in map\n\n";
 +  }
 +  try {
 +    students.at( Student("Minnie Mouse", 1240) ) = std::string("Walt Disney World Communications, P.O Box 10040, Lake Buena Vista, Florida 32830-0040 ");
 +  }
 +  catch(std::out_of_range& ex) {
 +    std::cout << "Student '" << Student("Minie Mouse", 1240) << "' does not exist in map\n\n";
 +  }
 +  
 +  print(students);
 +}
 +</code>
 +
 +
  
  
cpp/stl/map.1590913501.txt.gz · Last modified: 2020/05/31 07:25 (external edit)