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

C++ Builder

  1. #ifdef __BCPLUSPLUS__
  2. #include <iostream.h>
  3. #include <vector.h>
  4. #else
  5. #include <iostream>
  6. #include <vector>
  7. #endif
  8. using namespace std;
  9. typedef vector<int> INTVECTOR;
  10. void main(void)
  11.  {
  12.    // Dynamically allocated vector begins with 0 elements.
  13.    INTVECTOR theVector;
  14.    // Add one element to the end of the vector, an int with the value 42.
  15.    theVector.push_back(42);
  16.    // Show statistics about vector.
  17.    cout << "theVector's size is: " << theVector.size() << endl;
  18.    cout << "theVector's maximum size is: " << theVector.max_size()<< endl;
  19.    cout << "theVector's capacity is: " << theVector.capacity() << endl;
  20.    // Ensure there's room for at least 1000 elements.
  21.    theVector.reserve(1000);
  22.    cout << endl << "After reserving storage for 1000 elements:" << endl;
  23.    cout << "theVector's size is: " << theVector.size() << endl;
  24.    cout << "theVector's maximum size is: " << theVector.max_size()<< endl;
  25.    cout << "theVector's capacity is: " << theVector.capacity() << endl;
  26.    // Ensure there's room for at least 2000 elements.
  27.    theVector.resize(2000);
  28.    cout << endl << "After resizing storage to 2000 elements:" << endl;
  29.    cout << "theVector's size is: " << theVector.size() << endl;
  30.    cout << "theVector's maximum size is: " << theVector.max_size()<< endl;
  31.    cout << "theVector's capacity is: " << theVector.capacity() << endl;
  32.  }