cpp:stl:containers
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
cpp:stl:containers [2020/05/28 17:44] – [Αναζήτηση στοιχείου] gthanos | cpp:stl:containers [2022/05/26 16:49] (current) – gthanos | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Κοινές συναρτήσεις για όλους τους containers ====== | ||
- | ==== Εισαγωγή στοιχείου | + | ===== STL Containers ===== |
- | Με εξαίρεση | + | Στο παρακάτω διάγραμμα περιέχονται το σύνολο των //containers// που παρέχει η βιβλιοθήκη //STL//. |
- | Παραδείγματα: | + | {{ :cpp:stl:stl.png?800 |}} |
- | === A. Ένθεση σε λίστα === | + | |
- | <code cpp student_list_insert.cpp> | + | Οι //containers// της STL διακρίνονται στις εξής κατηγορίες: |
- | #include < | + | |
- | #include < | + | |
- | #include < | + | |
- | #include " | + | |
- | int main () { | + | * **Sequence containers: |
- | Student students[] = { Student(" | + | * **array:** Πίνακας σταθερού μεγέθους το οποίο προσδιορίζεται κατά τη δήλωση του πίνακα. |
- | + | * **vector:** Πίνακας μεταβλητού μεγέθους. Παρέχει δυνατότητα εισαγωγής στοιχείων σε οποιαδήποτε θέση του πίνακα. Η εισαγωγή/ | |
- | std::cerr << "----- Init list -----" << std::endl; | + | |
- | std:: | + | |
- | for(int i=0; i<2; i++) { | + | * **forward_list: |
- | | + | * **Container Adapters:** Χρησιμοποιούν εσωτερικά ένα |
- | | + | * **stack:** Υλοποιεί μία δομή Last-In-First-Out (LIFO) με την βοήθεια ενός //vector// ή // |
- | } | + | * **queue:** Υλοποιεί μία δομή First-In-First-Out (FIFO) με την βοήθεια ενός //deque//. |
- | + | | |
- | mylist.emplace(mylist.end(), " | + | |
- | + | | |
- | std::cerr << " | + | |
- | std::cerr << " | + | |
- | for (std:: | + | |
- | | + | |
- | | + | |
- | std::cerr << " | + | |
- | | + | |
- | return 0; | + | |
- | } | + | |
- | </code> | + | |
- | === Β. Ένθεση σε 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; | ||
- | } | ||
- | </ | ||
- | |||
- | ==== Αναζήτηση στοιχείου ==== | ||
- | |||
- | Η αναζήτηση ενός στοιχείου είναι διαφορετικά εάν αναζητούμε σε //sequence container// ή σε άλλου τύπου // | ||
- | |||
- | === Αναζήτηση σε sequence container === | ||
- | |||
- | H αναζήτηση σε sequence container γίνεται μέσω της συνάρτησης [[http:// | ||
- | |||
- | <code cpp find_in_vector.cpp> | ||
- | // find example | ||
- | #include < | ||
- | #include < | ||
- | #include < | ||
- | |||
- | int main () { | ||
- | // using std::find with array and pointer: | ||
- | int myints[] = { 10, 40, 30, 30, 40, 50, 30, 60, 30 }; | ||
- | int *p; | ||
- | int search_int; | ||
- | | ||
- | std::cout << "Enter an integer to search for: "; | ||
- | std::cin >> search_int; | ||
- | |||
- | p = std::find (myints, myints+9, search_int); | ||
- | | ||
- | if (p != myints+9) | ||
- | std::cout << "First occurance of " << search_int << " in myints: " << *p << ' | ||
- | else | ||
- | std::cout << search_int << " not found in myints: " << ' | ||
- | |||
- | std:: | ||
- | auto it = myvector.begin(); | ||
- | bool found = false; | ||
- | while(true) { | ||
- | it = find (it , myvector.end(), | ||
- | if (it != myvector.end()) { | ||
- | std::cout << search_int << " found in myvector at pos: " << it - myvector.begin() << ' | ||
- | it++; | ||
- | found = true; | ||
- | } else { | ||
- | if(!found) | ||
- | std::cout << search_int <<" | ||
- | break; | ||
- | } | ||
- | } | ||
- | |||
- | return 0; | ||
- | } | ||
- | </ | ||
- | |||
- | === Αναζήτηση σε associative container και unordered associative container === | ||
- | |||
- | Επειδή η συνάρτηση //find// ψάχνει γραμμικά μέσα στο περιεχόμενο του container δεν είναι βέλτιστή για containers που περιγράφονται από δέντρα ή από πίνακα κατακερματισμού. Αυτοί οι // | ||
- | |||
- | === Αναζήτηση σε set === | ||
- | |||
- | <code cpp find_in_set.cpp> | ||
- | #include < | ||
- | #include <set> | ||
- | |||
- | int main () | ||
- | { | ||
- | std:: | ||
- | std:: | ||
- | |||
- | // set some initial values: | ||
- | for (int i=1; i<=5; i++) myset.insert(i*10); | ||
- | |||
- | it=myset.find(20); | ||
- | myset.erase (it); | ||
- | myset.erase (myset.find(40)); | ||
- | |||
- | std::cout << "myset contains:"; | ||
- | for (it=myset.begin(); | ||
- | std::cout << ' ' << *it; | ||
- | std::cout << ' | ||
- | |||
- | return 0; | ||
- | } | ||
- | </ | ||
- | |||
- | |||
- | === Αναζήτηση σε unordered set === | ||
- | |||
- | // unordered_set:: | ||
- | #include < | ||
- | #include < | ||
- | #include < | ||
- | |||
- | template< | ||
- | void print_set(std:: | ||
- | std::cout << "myset contains:"; | ||
- | for (auto it=myset.begin(); | ||
- | std::cout << ' ' << *it; | ||
- | std::cout << ' | ||
- | } | ||
- | |||
- | int main () { | ||
- | std:: | ||
- | std:: | ||
- | char* word[] = { " | ||
- | |||
- | // unordered_set some initial values: | ||
- | for (int i=0; i<5; i++) | ||
- | myset.emplace(word[i]); | ||
- | | ||
- | print_set(myset); | ||
- | | ||
- | it=myset.find(std:: | ||
- | if(it != myset.end()) { | ||
- | myset.erase (it); | ||
- | std::cout << "' | ||
- | } | ||
- | else | ||
- | std::cout << "' | ||
- | | ||
- | print_set(myset); | ||
- | myset.erase (myset.find(std:: | ||
- | std::cout << "' | ||
- | print_set(myset); | ||
- | |||
- | return 0; | ||
- | } | ||
- | |||
- | <WRAP important 80% center round> | ||
- | </ | ||
- |
cpp/stl/containers.txt · Last modified: 2022/05/26 16:49 by gthanos