Ordered Collections

Software suppliers are trying to make their software packages more ‘user-friendly’… Their best approach so far has been to take all the old brochures and stamp the words ‘user-friendly’ on the cover. Bill Gates

vector

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

0 Comments


Definition

Vector is a container that hold dynamic size of arrays.

Character

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++)
		vec1.push_back(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;
}

						

Output

    					 
					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.

0 Comments



Please login to add comments.