vec6.cpp

We use cookies. Read the Privacy and Cookie Policy

vec6.cpp

#include ‹iostream.h›

#include ‹stl.h›

int array[] = {1, 4, 9, 16, 25, 36};

int main() {

 vector‹int› v(array, array + 6);

 for (int i = 0; i ‹ v.size(); i++) cout ‹‹ "v[" ‹‹ i ‹‹ "] = " ‹‹ v[i] ‹‹ endl;

 cout ‹‹ endl;

 v.erase(v.begin()); // Erase first element.

 for (i = 0; i ‹ v.size(); i++) cout ‹‹ "v[" ‹‹ i ‹‹ "] = " ‹‹ v[i] ‹‹ endl;

 cout ‹‹ endl;

 v.erase(v.end() - 1); // Erase last element.

 for (i = 0; i ‹ v.size(); i++) cout ‹‹ "v[" ‹‹ i ‹‹ "] = " ‹‹ v[i] ‹‹ endl;

 cout ‹‹ endl;

 v.erase(v.begin() + 1, v.end() - 1); // Erase all but first and last.

 for (i = 0; i ‹ v.size(); i++)

 cout ‹‹ "v[" ‹‹ i ‹‹ "] = " ‹‹ v[i] ‹‹ endl;

 cout ‹‹ endl;

 v.erase(); // Erase all.

 return 0;

}