User Tools

Site Tools


cpp:stl:containers

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Next revisionBoth sides next revision
cpp:stl:containers [2020/05/28 16:36] – [Διαγραφή στοιχείου] gthanoscpp:stl:containers [2020/05/28 17:04] gthanos
Line 85: Line 85:
   for (int i=1; i<10; ++i) mylist.push_back(i*10);   for (int i=1; i<10; ++i) mylist.push_back(i*10);
  
-                              // 10 20 30 40 50 60 70 80 90 +                                          // 10 20 30 40 50 60 70 80 90 
-  it1 = it2 = mylist.begin(); // ^^ +  it1 = it2 = mylist.begin();             // ^^ 
-  advance (it2,6);            // ^                 ^ +  advance (it2,6);                        // ^                 ^ 
-  ++it1;                      //    ^              ^+  ++it1;                                  //    ^              ^
  
-  it1 = mylist.erase (it1);   // 10 30 40 50 60 70 80 90 +  it1 = mylist.erase (it1);               // 10 30 40 50 60 70 80 90 
-                              //    ^           ^+                                          //    ^           ^
  
-  it2 = mylist.erase (it2);   // 10 30 40 50 60 80 90 +  it2 = mylist.erase (it2);               // 10 30 40 50 60 80 90 
-                              //    ^           ^ +                                          //    ^           ^ 
-                              // 10 30 40 50 60 80 90 +                                          // 10 30 40 50 60 80 90 
-  ++it1;                      //              ^ +  ++it1;                                  //              ^ 
-  --it2;                      //           ^+  --it2;                                  //           ^
  
-  it1=mylist.erase (it1,it2); // 10 30 60 80 90 +  it1=mylist.erase (it1,it2);             // 10 30 60 80 90 
-                              //        ^+                                          //        ^
  
   std::cout << "mylist contains:";   std::cout << "mylist contains:";
Line 128: Line 128:
   for (int i=1; i<10; i++) myset.insert(i*10);  // 10 20 30 40 50 60 70 80 90   for (int i=1; i<10; i++) myset.insert(i*10);  // 10 20 30 40 50 60 70 80 90
  
-  it = myset.begin(); +  it = myset.begin();                           // 10 20 30 40 50 60 70 80 90 
-  ++it;                                         // "it" points now to 20+                                                //  ^  
 +  ++it;                                         // 10 20 30 40 50 60 70 80 90 
 +                                                //     
  
-  myset.erase (it);+  myset.erase (it);                             // 10 30 40 50 60 70 80 90
  
-  myset.erase (40);+  myset.erase (40);                             // 10 30 50 60 70 80 90
  
-  it = myset.find (60); +  it = myset.find (60);                         // 10 30 50 60 70 80 90 
-  myset.erase (it, myset.end());+                                                //           ^ 
 +  myset.erase (it, myset.end());                // 10 30 50
  
   std::cout << "myset contains:";   std::cout << "myset contains:";
Line 146: Line 149:
 } }
 </code> </code>
 +
 +==== Αναζήτηση στοιχείου ====
 +
 +Η αναζήτηση ενός στοιχείου είναι διαφορετικά εάν αναζητούμε σε //sequence container// ή σε άλλου τύπου //container//.
 +
 +=== Αναζήτηση σε sequence container ===
 +
 +H αναζήτηση σε sequence container γίνεται μέσω της συνάρτησης [[http://www.cplusplus.com/reference/algorithm/find/|std::find]]. Όπως θα δείτε και από τον ενδεικτικό κώδικα στο link η συνάρτηση λειτουργεί διατρέχοντας τον //container// με την βοήθεια ενός //iterator// Το κόστος αναζήτησης είναι γραμμικό στο πλήθος των στοιχείων του //container// και για αυτό δεν αποτελεί βέλτιστη επιλογή για //associative// και //unordered associative containers//. Δείτε το παρακάτω ενδεικτικό παράδειγμα αναζήτησης σε μία λίστα. Η συνάρτηση [[http://www.cplusplus.com/reference/algorithm/find/|std::find]] επιστρέφει έναν //iterator// που δείχνει στην πρώτη θέση που εμφανίζεται το στοιχείο που αναζητούμε (μπορεί να εμφανίζεται σε περισσότερες θέσεις). Το παρακάτω  παράδειγμα είναι ενδεικτικό.
 +
 +
 +
 + 
cpp/stl/containers.txt · Last modified: 2022/05/26 16:49 by gthanos