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 revisionBoth sides next revision
cpp:stl:vector [2020/05/27 16:54] – [Διαγραφή των περιεχομένων του πίνακα] gthanoscpp:stl:vector [2020/05/27 16:56] gthanos
Line 133: Line 133:
     ints.erase(ints.begin());    // erase the first element.     ints.erase(ints.begin());    // erase the first element.
     ints.erase(ints.end()-1);    // erase the laste element.     ints.erase(ints.end()-1);    // erase the laste element.
 +    print(ints);
 +  }
 +}
 +</code>
 +
 +<code cpp vector_erase_03.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;
 +}
 +
 +template<typename T>
 +void print_r(vector<T> v) {
 +  for(auto it = v.crbegin(); it!=v.crend(); 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(), ints.end());    // erase everything!
     print(ints);     print(ints);
   }   }
cpp/stl/vector.txt · Last modified: 2023/05/29 19:12 by gthanos