vector1.cpp
Upload User: dq031136
Upload Date: 2022-08-08
Package Size: 802k
Code Size: 1k
Development Platform:

C++ Builder

  1. //  If you use Visual C++, set the compile options to /GX
  2. #ifdef __BCPLUSPLUS__
  3. #include <iostream.h>
  4. #include <vector.h>
  5. #else
  6. #include <iostream>
  7. #include <vector>
  8. #endif
  9. using namespace std;
  10. typedef vector<int> INTVECTOR;
  11. const ARRAY_SIZE = 4;
  12. void main(void)
  13. {
  14.    // Dynamically allocated vector begins with 0 elements.
  15.    INTVECTOR theVector;
  16.    // Intialize the array to contain the members [100, 200, 300, 400]
  17.    for (int cEachItem = 0; cEachItem < ARRAY_SIZE; cEachItem++)
  18.       theVector.push_back((cEachItem + 1) * 100);
  19.    cout << "First element: " << theVector.front() << endl;
  20.    cout << "Last element: " << theVector.back() << endl;
  21.    cout << "Elements in vector: " << theVector.size() << endl;
  22.    // Delete the last element of the vector. Remember that the vector
  23.    // is 0-based, so theVector.end() actually points 1 element beyond
  24.    // the end.
  25.    cout << "Deleting last element." << endl;
  26.    theVector.erase(theVector.end() - 1);
  27.    cout << "New last element is: " << theVector.back() << endl;
  28.    // Delete the first element of the vector.
  29.    cout << "Deleting first element." << endl;
  30.    theVector.erase(theVector.begin());
  31.    cout << "New first element is: " << theVector.front() << endl;
  32.    cout << "Elements in vector: " << theVector.size() << endl;
  33.  }