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 16:40] – [Διαγραφή στοιχείου] gthanos | cpp:stl:containers [2020/05/28 17:51] – [Αναζήτηση στοιχείου] gthanos | ||
---|---|---|---|
Line 149: | Line 149: | ||
} | } | ||
</ | </ | ||
+ | |||
+ | ==== Αναζήτηση στοιχείου ==== | ||
+ | |||
+ | Η αναζήτηση ενός στοιχείου είναι διαφορετικά εάν αναζητούμε σε //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 === | ||
+ | |||
+ | <code cpp find_in_unordered_set.cpp> | ||
+ | // 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> | ||
+ | Όλες οι παραλλαγές της συνάρτησης find επιστρέφουν έναν // | ||
+ | </ | ||
+ |
cpp/stl/containers.txt · Last modified: 2022/05/26 16:49 by gthanos