cpp:stl:containers
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revisionNext revisionBoth sides next revision | ||
cpp:stl:containers [2020/05/28 15:16] – created gthanos | cpp:stl:containers [2020/05/28 16:40] – [Διαγραφή στοιχείου] gthanos | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Κοινές συναρτήσεις για όλους τους containers ====== | ====== Κοινές συναρτήσεις για όλους τους containers ====== | ||
- | ==== Εισαγωγή στοιχείου | + | ==== Εισαγωγή στοιχείου ==== |
- | Με εξαίρεση την κλάση [[array|std:: | + | Με εξαίρεση την κλάση [[array|std:: |
- | + | ||
- | Επιπλέον, | + | |
Παραδείγματα: | Παραδείγματα: | ||
+ | === A. Ένθεση σε λίστα === | ||
<code cpp student_list_insert.cpp> | <code cpp student_list_insert.cpp> | ||
#include < | #include < | ||
#include < | #include < | ||
- | #include < | + | #include < |
#include " | #include " | ||
Line 38: | Line 37: | ||
</ | </ | ||
+ | === Β. Ένθεση σε set === | ||
+ | |||
+ | <code cpp string_set_insert.cpp> | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | int main () { | ||
+ | std::string strings[] = { std:: | ||
+ | | ||
+ | std::cerr << "----- Init set -----" << std::endl; | ||
+ | std:: | ||
+ | for(int i=0; i<3; i++) | ||
+ | myset.insert(strings[i]); | ||
+ | | ||
+ | myset.emplace(" | ||
+ | | ||
+ | std::cerr << " | ||
+ | std::cerr << "myset contains:"; | ||
+ | for (std:: | ||
+ | std::cerr << ' ' << *it; | ||
+ | std::cerr << std::endl; | ||
+ | std::cerr << " | ||
+ | | ||
+ | return 0; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ==== Διαγραφή στοιχείου ==== | ||
+ | |||
+ | Η διαγραφή στοιχείου είναι ανάλογη της εισαγωγής και γίνεται μέσω της συνάρτησης //erase//. H συνάρτηση επιστρέφει έναν // | ||
+ | |||
+ | === Διαγραφή από λίστα === | ||
+ | |||
+ | Το παρακάτω παράδειγμα είναι από τη σελίδα [[http:// | ||
+ | |||
+ | <code cpp list_erase.cpp> | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | int main () { | ||
+ | std:: | ||
+ | std:: | ||
+ | |||
+ | // insert values: | ||
+ | for (int i=1; i<10; ++i) mylist.push_back(i*10); | ||
+ | |||
+ | // 10 20 30 40 50 60 70 80 90 | ||
+ | it1 = it2 = mylist.begin(); | ||
+ | advance (it2, | ||
+ | ++it1; | ||
+ | |||
+ | it1 = mylist.erase (it1); | ||
+ | // ^ ^ | ||
+ | |||
+ | it2 = mylist.erase (it2); | ||
+ | // ^ ^ | ||
+ | // 10 30 40 50 60 80 90 | ||
+ | ++it1; | ||
+ | --it2; | ||
+ | |||
+ | it1=mylist.erase (it1, | ||
+ | // ^ | ||
+ | |||
+ | std::cout << " | ||
+ | for (it1=mylist.begin(); | ||
+ | std::cout << ' ' << *it1; | ||
+ | std::cout << ' | ||
+ | |||
+ | return 0; | ||
+ | </ | ||
+ | |||
+ | |||
+ | === Διαγραφή από set === | ||
+ | |||
+ | Το παρακάτω παράδειγμα είναι από τη σελίδα [[http:// | ||
+ | |||
+ | <code cpp set_erase.cpp> | ||
+ | // erasing from set | ||
+ | #include < | ||
+ | #include <set> | ||
+ | |||
+ | int main () | ||
+ | { | ||
+ | std:: | ||
+ | std:: | ||
+ | |||
+ | // insert some values: | ||
+ | for (int i=1; i<10; i++) myset.insert(i*10); | ||
+ | |||
+ | it = myset.begin(); | ||
+ | // ^ | ||
+ | ++it; // 10 20 30 40 50 60 70 80 90 | ||
+ | // | ||
+ | |||
+ | myset.erase (it); // 10 30 40 50 60 70 80 90 | ||
+ | |||
+ | myset.erase (40); // 10 30 50 60 70 80 90 | ||
+ | |||
+ | it = myset.find (60); // 10 30 50 60 70 80 90 | ||
+ | // ^ | ||
+ | myset.erase (it, myset.end()); | ||
+ | |||
+ | std::cout << "myset contains:"; | ||
+ | for (it=myset.begin(); | ||
+ | std::cout << ' ' << *it; | ||
+ | std::cout << ' | ||
+ | |||
+ | return 0; | ||
+ | } | ||
+ | </ |
cpp/stl/containers.txt · Last modified: 2022/05/26 16:49 by gthanos