vector2.cpp

来自「《C/C++程序员实用大全》配套代码,适用初学C++的人员。」· C++ 代码 · 共 39 行

CPP
39
字号
#ifdef __BCPLUSPLUS__
#include <iostream.h>
#include <vector.h>
#else
#include <iostream>
#include <vector>
#endif

using namespace std;
typedef vector<int> INTVECTOR;

void main(void)
 {
   // Dynamically allocated vector begins with 0 elements.
   INTVECTOR theVector;

   // Add one element to the end of the vector, an int with the value 42.
   theVector.push_back(42);

   // Show statistics about vector.
   cout << "theVector's size is: " << theVector.size() << endl;
   cout << "theVector's maximum size is: " << theVector.max_size()<< endl;
   cout << "theVector's capacity is: " << theVector.capacity() << endl;

   // Ensure there's room for at least 1000 elements.
   theVector.reserve(1000);
   cout << endl << "After reserving storage for 1000 elements:" << endl;
   cout << "theVector's size is: " << theVector.size() << endl;
   cout << "theVector's maximum size is: " << theVector.max_size()<< endl;
   cout << "theVector's capacity is: " << theVector.capacity() << endl;

   // Ensure there's room for at least 2000 elements.
   theVector.resize(2000);
   cout << endl << "After resizing storage to 2000 elements:" << endl;
   cout << "theVector's size is: " << theVector.size() << endl;
   cout << "theVector's maximum size is: " << theVector.max_size()<< endl;
   cout << "theVector's capacity is: " << theVector.capacity() << endl;
 }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?