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:02]
gthanos [Η κλάση std::pair]
cpp:stl:map [2020/05/31 08:25]
gthanos [H συνάρτηση operator[]]
Line 53: Line 53:
 </WRAP> </WRAP>
  
-Οι συναρτήσεις [[cpp:stl:set#%CE%BF%CE%B9_%CF%83%CF%85%CE%BD%CE%B1%CF%81%CF%84%CE%AE%CF%83%CE%B5%CE%B9%CF%82_lower_bound_upper_bound_%CE%BA%CE%B1%CE%B9_equal_range|lower_bound, upper_bound και equal_range]] υπάρχουν και για τις κλάσεις [[cpp:stl:map| std::map και std::multimap]]. Η λογική των συναρτήσεων αυτών είναι όμοια με το [[cpp:stl:set|std::set]] με την διαφορά ότι η αναζήτηση γίνεται στα κλειδιά του //std::map//. Το παρακάτω παράδειγμα είναι από την σελίδα  +===== Ένθεση και διάτρεξη ενός std::map ή std::multimap =====
- +
-<code cpp map_upper_lower_bound.cpp> +
-// map::lower_bound/upper_bound +
-#include <iostream> +
-#include <map> +
- +
-int main () +
-+
-  std::map<char,int> mymap; +
-  std::map<char,int>::iterator itlow,itup; +
- +
-  mymap['a']=20; +
-  mymap['b']=40; +
-  mymap['c']=60; +
-  mymap['d']=80; +
-  mymap['e']=100; +
- +
-  itlow=mymap.lower_bound ('b');  // itlow points to b +
-  itup=mymap.upper_bound ('d');   // itup points to e (not d!) +
- +
-  mymap.erase(itlow,itup);        // erases [itlow,itup) +
- +
-  // print content: +
-  for (std::map<char,int>::iterator it=mymap.begin(); it!=mymap.end(); ++it) +
-    std::cout << it->first << " => " << it->second << '\n'; +
- +
-  return 0; +
-+
-</code> +
- +
-===== Παραδείγματα =====+
  
 <code cpp color_map.cpp> <code cpp color_map.cpp>
Line 126: Line 95:
 Αλλάξτε την κλάση //std::map// σε //std::multimap// και παρατηρήστε τις αλλαγές στην εκτύπωση. Αλλάξτε την κλάση //std::map// σε //std::multimap// και παρατηρήστε τις αλλαγές στην εκτύπωση.
 </WRAP> </WRAP>
 +
 +===== Οι συναρτήσεις operator[] και at =====
 +
 +==== H συνάρτηση operator[] ====
 +
 +Η συνάρτηση [[http://www.cplusplus.com/reference/map/map/operator[]| operator[] ]] έχει διπλό ρόλο //α)// να επιστρέψει την τιμή ενός κλειδιού που είναι αποθηκευμένο στο //std::map// ή //std::multimap// και //β)// να θέσει την τιμή ενός κλειδιού που είναι ή δεν είναι αποθηκευμένο sto //std::map//, //std::multimap//.
 +
 +=== Παράδειγμα 1ο - Ανάγνωση με χρήση του operator [] ===
 +
 +<code cpp student_map_read_using_op.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 = students[ Student("Peter Pan", 1234) ];     // address_pp exists
 +  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 << "Minie Mouse aem: " << aem_mm << std::endl;             // empty string
 +}
 +</code>
 +
 +=== Παράδειγμα 2ο - Εγγραφή με χρήση του operator [] ===
 +
 +<code cpp student_map_write_using_op.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 );
 +  
 +  students[ Student("Peter Pan", 1234) ] = std::string("Home Under The Ground, Neverland");
 +  students[ Student("Minnie Mouse", 1240) ] = std::string("Walt Disney World Communications, P.O Box 10040, Lake Buena Vista, Florida 32830-0040 ");
 +  
 +  print(students);
 +}
 +</code>
 +
 +
 +
 +
 +
 +===== Οι συναρτήσεις lower_bound, upper_bound και equal_range =====
 +
 +Οι συναρτήσεις [[cpp:stl:set#%CE%BF%CE%B9_%CF%83%CF%85%CE%BD%CE%B1%CF%81%CF%84%CE%AE%CF%83%CE%B5%CE%B9%CF%82_lower_bound_upper_bound_%CE%BA%CE%B1%CE%B9_equal_range|lower_bound, upper_bound και equal_range]] υπάρχουν και για τις κλάσεις [[cpp:stl:map| std::map και std::multimap]]. Η λογική των συναρτήσεων αυτών είναι όμοια με το [[cpp:stl:set|std::set]] με την διαφορά ότι η αναζήτηση γίνεται στα κλειδιά του //std::map//. Το παρακάτω παράδειγμα είναι από την σελίδα [[http://www.cplusplus.com/reference/map/map/lower_bound/|cplusplus.com]].
 +
 +<code cpp map_upper_lower_bound.cpp>
 +// map::lower_bound/upper_bound
 +#include <iostream>
 +#include <map>
 +
 +int main ()
 +{
 +  std::map<char,int> mymap;
 +  std::map<char,int>::iterator itlow,itup;
 +
 +  mymap['a']=20;
 +  mymap['b']=40;
 +  mymap['c']=60;
 +  mymap['d']=80;
 +  mymap['e']=100;
 +
 +  itlow=mymap.lower_bound ('b');  // itlow points to b
 +  itup=mymap.upper_bound ('d');   // itup points to e (not d!)
 +
 +  mymap.erase(itlow,itup);        // erases [itlow,itup)
 +
 +  // print content:
 +  for (std::map<char,int>::iterator it=mymap.begin(); it!=mymap.end(); ++it)
 +    std::cout << it->first << " => " << it->second << '\n';
 +
 +  return 0;
 +}
 +</code>
  
  
  
  
cpp/stl/map.txt · Last modified: 2020/06/01 06:13 (external edit)