cpp:stl:map

Differences

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

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Next revision Both sides next revision
cpp:stl:map [2020/05/31 08:23]
gthanos
cpp:stl:map [2020/05/31 09:22]
gthanos [H συνάρτηση operator[]]
Line 100: Line 100:
 ==== H συνάρτηση operator[] ==== ==== H συνάρτηση operator[] ====
  
-Η συνάρτηση [[http://www.cplusplus.com/reference/map/map/operator[]/operator[] ]] έχει διπλό ρόλο //α)// να επιστρέψει την τιμή ενός κλειδιού που είναι αποθηκευμένο στο //std::map// ή //std::multimap// και //β)// να θέσει την τιμή ενός κλειδιού που είναι ή δεν είναι αποθηκευμένο sto //std::map//, //std::multimap//.+Η συνάρτηση [[http://www.cplusplus.com/reference/map/map/operator[]operator[] ]] έχει διπλό ρόλο //α)// να επιστρέψει την τιμή ενός κλειδιού που είναι αποθηκευμένο στο //std::map// ή //std::multimap// και //β)// να θέσει την τιμή ενός κλειδιού που είναι ή δεν είναι αποθηκευμένο sto //std::map//, //std::multimap//.
  
 === Παράδειγμα 1ο - Ανάγνωση με χρήση του operator [] === === Παράδειγμα 1ο - Ανάγνωση με χρήση του operator [] ===
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.txt · Last modified: 2020/06/01 06:13 (external edit)