Demonstrating the STL vector capacity : vector capacity « vector « C++ Tutorial

Home
C++ Tutorial
1.Language Basics
2.Data Types
3.Operators statements
4.Array
5.Development
6.Exceptions
7.Function
8.Structure
9.Class
10.Operator Overloading
11.Pointer
12.File Stream
13.template
14.STL Introduction
15.string
16.vector
17.list
18.bitset
19.set multiset
20.valarray
21.queue stack
22.deque
23.map multimap
24.STL Algorithms Modifying sequence operations
25.STL Algorithms Non modifying sequence operations
26.STL Algorithms Binary search
27.STL Algorithms Sorting
28.STL Algorithms Merge
29.STL Algorithms Min Max
30.STL Algorithms Iterator
31.STL Algorithms Heap
32.STL Algorithms Helper
C / ANSI-C
C Tutorial
C++
Visual C++ .NET
C++ Tutorial » vector » vector capacity 
16.7.4.Demonstrating the STL vector capacity
#include <iostream>
#include <cassert>
#include <vector>
using namespace std;

int main()
{
  int N = 10000// size of vectors

  vector<int> vector1, vector2;

  for (int k = 0; k != N; ++k) {
    vector<int>::size_type cap = vector1.capacity();
    vector1.push_back(k);
    if (vector1.capacity() != cap)
      cout << "k: " << k << ", new capacity: " << vector1.capacity() << endl;
  }

  return 0;
}
k: 0, new capacity: 1
k: 1, new capacity: 2
k: 2, new capacity: 4
k: 4, new capacity: 8
k: 8, new capacity: 16
k: 16, new capacity: 32
k: 32, new capacity: 64
k: 64, new capacity: 128
k: 128, new capacity: 256
k: 256, new capacity: 512
k: 512, new capacity: 1024
k: 1024, new capacity: 2048
k: 2048, new capacity: 4096
k: 4096, new capacity: 8192
k: 8192, new capacity: 16384
16.7.vector capacity
16.7.1.make a big vector and then deallocate all its memory
16.7.2.make a big vector and then minimize its memory
16.7.3.make the vector as large as possible without reallocating
16.7.4.Demonstrating the STL vector capacity
16.7.5.Demonstrating the STL vector capacity and reserve functions
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.