This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision Next revision Both sides next revision | ||
|
cpp:stl:map [2020/05/31 07:21] gthanos [Παραδείγματα] |
cpp:stl:map [2020/05/31 08:25] gthanos [H συνάρτηση operator[]] |
||
|---|---|---|---|
| Line 53: | Line 53: | ||
| </ | </ | ||
| - | ===== Παραδείγματα | + | ===== Ένθεση και διάτρεξη ενός std::map ή std:: |
| <code cpp color_map.cpp> | <code cpp color_map.cpp> | ||
| Line 95: | Line 95: | ||
| Αλλάξτε την κλάση // | Αλλάξτε την κλάση // | ||
| </ | </ | ||
| + | |||
| + | ===== Οι συναρτήσεις operator[] και at ===== | ||
| + | |||
| + | ==== H συνάρτηση operator[] ==== | ||
| + | |||
| + | Η συνάρτηση [[http:// | ||
| + | |||
| + | === Παράδειγμα 1ο - Ανάγνωση με χρήση του operator [] === | ||
| + | |||
| + | <code cpp student_map_read_using_op.cpp> | ||
| + | #include <map> | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | #include " | ||
| + | |||
| + | int main() { | ||
| + | std:: | ||
| + | | ||
| + | std:: | ||
| + | | ||
| + | std:: | ||
| + | students.insert( peter_pan ); | ||
| + | students.insert( mickey_mouse ); | ||
| + | | ||
| + | std::string address_pp = students[ Student(" | ||
| + | std::string address_mm = students[ Student(" | ||
| + | | ||
| + | std::cout << "Peter Pan aem: " << aem_pp << std::endl; | ||
| + | std::cout << "Minie Mouse aem: " << aem_mm << std:: | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | === Παράδειγμα 2ο - Εγγραφή με χρήση του operator [] === | ||
| + | |||
| + | <code cpp student_map_write_using_op.cpp> | ||
| + | #include <map> | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | #include " | ||
| + | |||
| + | template< | ||
| + | void print(std:: | ||
| + | for(auto it = s.cbegin(); it!=s.cend(); | ||
| + | std::cout << it-> | ||
| + | } | ||
| + | |||
| + | int main() { | ||
| + | std:: | ||
| + | | ||
| + | std:: | ||
| + | | ||
| + | std:: | ||
| + | students.insert( peter_pan ); | ||
| + | students.insert( mickey_mouse ); | ||
| + | | ||
| + | students[ Student(" | ||
| + | students[ Student(" | ||
| + | | ||
| + | print(students); | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | |||
| + | |||
| + | |||
| + | |||
| + | ===== Οι συναρτήσεις lower_bound, | ||
| + | |||
| + | Οι συναρτήσεις [[cpp: | ||
| + | |||
| + | <code cpp map_upper_lower_bound.cpp> | ||
| + | // map:: | ||
| + | #include < | ||
| + | #include <map> | ||
| + | |||
| + | int main () | ||
| + | { | ||
| + | std:: | ||
| + | std:: | ||
| + | |||
| + | mymap[' | ||
| + | mymap[' | ||
| + | mymap[' | ||
| + | mymap[' | ||
| + | mymap[' | ||
| + | |||
| + | itlow=mymap.lower_bound (' | ||
| + | itup=mymap.upper_bound (' | ||
| + | |||
| + | mymap.erase(itlow, | ||
| + | |||
| + | // print content: | ||
| + | for (std:: | ||
| + | std::cout << it-> | ||
| + | |||
| + | return 0; | ||
| + | } | ||
| + | </ | ||