Ordered Collections

As soon as we started programming, we found to our surprise that it wasnít as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs. Maurice Wilkes


Language C++ | Level Intermediate | Category Standard Template Library (STL) | 2015-11-16 22:25:26



Vector is a container that hold dynamic size of arrays.


small class containing pointers into the heap.
Dynamic array and allocate memory dynamically.
Vectors are efficient and flexible.
Vector grows automatically when require more memory.

source Code

#include "stdafx.h"

#include < iostream >
#include < vector >
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
	vector < int > vec1;

	cout << "Store items in the vector" << endl;
	for (int i = 0; i < 5; i++)

	cout << "Display items from Vector" << endl;
	for (unsigned int i = 0; i < vec1.size(); i++)
		cout << vec1[i] <<"\t";

	cout << endl;

	vector < int >::reverse_iterator reverseiter;
	cout << "Display items from Vector using reverse iterator" << endl;
	for (reverseiter = vec1.rbegin(); reverseiter != vec1.rend(); ++reverseiter)
		cout << *reverseiter << "\t";
	cout << endl;

	cout << "Size of Vector : " << vec1.size() << endl;

	return 0;



					Store items in the vector
					Display items from Vector
					0       1       2       3       4
					Display items from Vector using reverse iterator
					4       3       2       1       0
					Size of Vector : 5

When to use

When you donít know the size in advance, you can use vector.


Please login to add comments.