cpp:stl:containers
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
cpp:stl:containers [2020/05/28 15:25] – gthanos | cpp:stl:containers [2020/05/28 16:40] – [Διαγραφή στοιχείου] gthanos | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Κοινές συναρτήσεις για όλους τους containers ====== | ====== Κοινές συναρτήσεις για όλους τους containers ====== | ||
- | ==== Εισαγωγή στοιχείου | + | ==== Εισαγωγή στοιχείου ==== |
Με εξαίρεση την κλάση [[array|std:: | Με εξαίρεση την κλάση [[array|std:: | ||
Line 66: | Line 66: | ||
</ | </ | ||
+ | ==== Διαγραφή στοιχείου ==== | ||
+ | |||
+ | Η διαγραφή στοιχείου είναι ανάλογη της εισαγωγής και γίνεται μέσω της συνάρτησης //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