Reserve space for 50 elements in vector - C++ STL

C++ examples for STL:vector

Description

Reserve space for 50 elements in vector

Demo Code

#include <iostream>
#include <vector>
using namespace std;
void show(const char *msg, vector<int> vect);
int main() {//from w  ww .j  a  v a2  s.co  m
   vector<int> v(10);
   for(unsigned i=0; i < v.size(); ++i)
      v[i] = i*i;
   show("Contents of v: ", v);
   // Now, reserve space for 50 elements.
   v.reserve(50);
   cout << "After calling reserve(50), the size of v is "
   << v.size() << " and the capacity is "
   << v.capacity() << ".\n";
   return 0;
}
void show(const char *msg, vector<int> vect) {
   cout << msg;
   for(unsigned i=0; i < vect.size(); ++i)
      cout << vect[i] << " ";
   cout << "\n";
}

Result


Related Tutorials