User Tools

Site Tools


cpp:stl:vector

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:vector [2020/05/27 16:54] – [Ένθεση στον πίνακα] gthanoscpp:stl:vector [2020/05/27 16:56] gthanos
Line 108: Line 108:
  
 <code cpp vector_erase_02.cpp> <code cpp vector_erase_02.cpp>
 +#include <vector>
 +#include <list>
 +#include <iostream>
 +#include <iomanip>
 +
 +using namespace std;
 +
 +template<typename T>
 +void print(vector<T> v) {
 +  for(auto it = v.cbegin(); it!=v.cend(); it++) 
 +    cout << setw(3) << *it;
 +  cout << endl;
 +}
 +
 +int main() {
 +  vector<int> ints;                  // initially zero size
 +  for(int i=0; i<10; i++) {
 +    ints.push_back(i+1);             // insert to the end - very fast
 +    ints.insert(ints.begin(),i+1);   // insert to the beginning - extremely slow
 +  }
 +  print(ints);
 +  
 +  while(ints.size() > 0) {
 +    ints.erase(ints.begin());    // erase the first element.
 +    ints.erase(ints.end()-1);    // erase the laste element.
 +    print(ints);
 +  }
 +}
 +</code>
 +
 +<code cpp vector_erase_03.cpp>
 #include <vector> #include <vector>
 #include <list> #include <list>
Line 138: Line 169:
      
   while(ints.size() > 0) {   while(ints.size() > 0) {
-    ints.erase(ints.begin());    // erase the first element. +    ints.erase(ints.begin()ints.end());    // erase everything!
-    ints.erase(ints.end()-1);    // erase the laste element.+
     print(ints);     print(ints);
   }   }
cpp/stl/vector.txt · Last modified: 2023/05/29 19:12 by gthanos