Ordered Collections

Most good programmers do programming not because they expect to get paid or get adulation by the public, but because it is fun to program. Linus Torvalds

array

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

0 Comments


Definition

The array is a container that hold fixed size of arrays.

Character

Class version of the classic C array.
Size fixed during compile time.
It has better performance compare than vector.
Array type has a default constructor array().

source Code

								
#include "stdafx.h"

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

void display(array < int, 5 > array1)
{
	array < int, 5 >::iterator iter;

	for (iter = array1.begin(); iter != array1.end(); iter++)
		cout << *iter << " ";

	cout << endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
	cout << "Store the items in array" << endl;
	array < int, 5 > array1;

	for (int i = 4; i >= 0; i--)
		array1[i] = i;


	cout << "Display items from array :";
	display(array1);
	
	sort(array1.begin(), array1.end());
	cout << "Sort items int array :";
	display(array1);

	array < int, 5 > array2= { 5, 6, 7, 8, 9 };
	swap(array1,array2);
	cout << "Display items from array after swap the items:";
	display(array1);


	return 0;
}

						

Output

    					 
					Store the items in array
					Display items from array: 0 1 2 3 4
					Sort items int array :0 1 2 3 4
					Display items from array after swap the items: 5 6 7 8 9
					    					 
					    

When to use

When you know the size during compile time, you can use array.

0 Comments



Please login to add comments.