cpp:stl:vector
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:vector [2020/05/27 16:30] – [Ένθεση στον πίνακα] gthanos | cpp:stl:vector [2020/05/27 17:48] – [std::vector] gthanos | ||
---|---|---|---|
Line 4: | Line 4: | ||
{{ : | {{ : | ||
+ | |||
+ | **Σημείωση: | ||
===== Επίδοσης της δομής ===== | ===== Επίδοσης της δομής ===== | ||
Line 11: | Line 13: | ||
* Η πράξη της αναζήτησης είναι γραμμική στο μέγεθος των στοιχείων του πίνακα **(Ο(Ν))**. | * Η πράξη της αναζήτησης είναι γραμμική στο μέγεθος των στοιχείων του πίνακα **(Ο(Ν))**. | ||
- | ==== Ένθεση στον πίνακα ==== | + | ===== Πρόσβαση στα στοιχεία του πίνακα ===== |
+ | |||
+ | Για τη συνάρτηση //vector// ισχύει η πρόσβαση μέσω των συναρτήσεων [[http:// | ||
+ | |||
+ | <code cpp vector_access.cpp> | ||
+ | #include < | ||
+ | #include < | ||
+ | using namespace std; | ||
+ | |||
+ | int main () { | ||
+ | vector< | ||
+ | cout << "size: " << v.size() << endl; | ||
+ | cout << "capa: " << v.capacity() << endl; | ||
+ | |||
+ | try { | ||
+ | v.at(3) = 600; | ||
+ | cout << " | ||
+ | v.at(4) = 1; | ||
+ | cout << " | ||
+ | } catch(std:: | ||
+ | cout << ex.what() << endl; | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== Ένθεση στον πίνακα | ||
Όπως προαναφέρθηκε, | Όπως προαναφέρθηκε, | ||
Line 19: | Line 46: | ||
#include < | #include < | ||
#include < | #include < | ||
+ | #include < | ||
using namespace std; | using namespace std; | ||
Line 25: | Line 53: | ||
void print(vector< | void print(vector< | ||
for(auto it = v.cbegin(); it!=v.cend(); | for(auto it = v.cbegin(); it!=v.cend(); | ||
- | cout << setw(4) << *it; | + | cout << setw(3) << *it; |
cout << endl; | cout << endl; | ||
} | } | ||
+ | |||
+ | int main() { | ||
+ | vector< | ||
+ | cout << "size: " << setw(2) << ints.size() ; | ||
+ | cout << " capacity: " << setw(2) << ints.capacity() << endl; | ||
+ | for(int i=0; i<10; i++) { | ||
+ | ints.push_back(i+1); | ||
+ | cout << "size: " << setw(2) << ints.size() ; | ||
+ | cout << " capacity: " << setw(2) << ints.capacity() << endl; | ||
+ | ints.insert(ints.begin(), | ||
+ | cout << "size: " << setw(2) << ints.size() ; | ||
+ | cout << " capacity: " << setw(2) << ints.capacity() << endl; | ||
+ | } | ||
+ | print(ints); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Η εκτύπωση του παραπάνω προγράμματος όσον αφορά το μέγεθος και τη χωρητικότητα του πίνακα είναι η εξής: | ||
+ | < | ||
+ | size: 0 capacity: | ||
+ | size: 1 capacity: | ||
+ | size: 2 capacity: | ||
+ | size: 3 capacity: | ||
+ | size: 4 capacity: | ||
+ | size: 5 capacity: | ||
+ | size: 6 capacity: | ||
+ | size: 7 capacity: | ||
+ | size: 8 capacity: | ||
+ | size: 9 capacity: 16 | ||
+ | size: 10 capacity: 16 | ||
+ | size: 11 capacity: 16 | ||
+ | size: 12 capacity: 16 | ||
+ | size: 13 capacity: 16 | ||
+ | size: 14 capacity: 16 | ||
+ | size: 15 capacity: 16 | ||
+ | size: 16 capacity: 16 | ||
+ | size: 17 capacity: 32 | ||
+ | size: 18 capacity: 32 | ||
+ | size: 19 capacity: 32 | ||
+ | size: 20 capacity: 32 | ||
+ | </ | ||
+ | |||
+ | Παρατηρήστε ότι κάθε φορά που χρειάζεται να γίνει μεγένθυση του πίνακα (re-allocation) η χωρητικότητα διπλασιάζεται. Το παραπάνω εξασφαλίζει οτι το επιμέρισμένο κόστος αντιγραφής για κάθε re-allocation είναι σταθερό Ο(1) (δες σχετικά την ανάλυση της επίδοσης του ανακατακερματισμού στο βιβλίο των Δομών Δεδομένων του κ. Μποζάνη). | ||
+ | |||
+ | ===== Διαγραφή των περιεχομένων του πίνακα ===== | ||
+ | |||
+ | Η διαγραφή των περιεχομένων ενός της κλάσης //vector// μπορεί να γίνει μέσω της μεθόδου [[http:// | ||
+ | |||
+ | * '' | ||
+ | * '' | ||
+ | |||
+ | <WRAP important 80% center round> | ||
+ | Όταν μία συνάρτηση της STL λαμβάνει ως παραμέτρους δύο // | ||
+ | </ | ||
+ | |||
+ | <code cpp vector_erase_01.cpp> | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | using namespace std; | ||
template< | template< | ||
- | void print_r(vector< | + | void print(vector< |
- | for(auto it = v.crbegin(); it!=v.crend(); it++) | + | for(auto it = v.cbegin(); it!=v.cend(); it++) |
- | cout << setw(4) << *it; | + | cout << setw(3) << *it; |
cout << endl; | cout << endl; | ||
} | } | ||
Line 43: | Line 133: | ||
} | } | ||
print(ints); | print(ints); | ||
+ | | ||
+ | while(ints.size() > 0) { | ||
+ | auto pos = ints.begin() + ints.size()/ | ||
+ | ints.erase(pos, | ||
+ | print(ints); | ||
+ | } | ||
} | } | ||
+ | |||
</ | </ | ||
+ | |||
+ | <code cpp vector_erase_02.cpp> | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | using namespace std; | ||
+ | |||
+ | template< | ||
+ | void print(vector< | ||
+ | for(auto it = v.cbegin(); it!=v.cend(); | ||
+ | cout << setw(3) << *it; | ||
+ | cout << endl; | ||
+ | } | ||
+ | |||
+ | int main() { | ||
+ | vector< | ||
+ | for(int i=0; i<10; i++) { | ||
+ | ints.push_back(i+1); | ||
+ | ints.insert(ints.begin(), | ||
+ | } | ||
+ | print(ints); | ||
+ | | ||
+ | while(ints.size() > 0) { | ||
+ | ints.erase(ints.begin()); | ||
+ | ints.erase(ints.end()-1); | ||
+ | print(ints); | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | <code cpp vector_erase_03.cpp> | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | using namespace std; | ||
+ | |||
+ | template< | ||
+ | void print(vector< | ||
+ | for(auto it = v.cbegin(); it!=v.cend(); | ||
+ | cout << setw(3) << *it; | ||
+ | cout << endl; | ||
+ | } | ||
+ | |||
+ | int main() { | ||
+ | vector< | ||
+ | for(int i=0; i<10; i++) { | ||
+ | ints.push_back(i+1); | ||
+ | ints.insert(ints.begin(), | ||
+ | } | ||
+ | print(ints); | ||
+ | | ||
+ | while(ints.size() > 0) { | ||
+ | ints.erase(ints.begin(), | ||
+ | print(ints); | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== Ανάθεση των περιεχομένων του vector από οποιοδήποτε άλλο container ===== | ||
+ | |||
+ | Η ανάθεση των περιεχομένων ενός //vector// μέσω της συνάρτησης // | ||
+ | |||
+ | <code cpp vector_assign.cpp> | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | using namespace std; | ||
+ | |||
+ | template< | ||
+ | void print(vector< | ||
+ | for(auto it = v.cbegin(); it!=v.cend(); | ||
+ | cout << setw(3) << *it; | ||
+ | cout << endl; | ||
+ | } | ||
+ | |||
+ | int main () { | ||
+ | vector< | ||
+ | array< | ||
+ | |||
+ | v.assign(a.begin(), | ||
+ | | ||
+ | print(v); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | |||
cpp/stl/vector.txt · Last modified: 2023/05/29 19:12 by gthanos