Ordered Collections

There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult. C.A.R. Hoare

list

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

0 Comments


Definition

List is a container implemented on doubly linked list. It can be iterated both forwards and backwards.

Character

It allow to insert and delete any locations in list.
List container implemented based on doubly linked list.
It doesn’t support random access. It Can not access the items using index.
Lists perform better in inserting, extracting and moving elements in any position within the container.

source Code

								
#include "stdafx.h"

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

int _tmain(int argc, _TCHAR* argv[])
{
	list < int > list1;

	cout << "Store items in list" << endl;

	for (int i = 0; i < 10; i++)
		list1.push_back(i);

	list < int >::iterator iter;
	cout << "Display items from list : " ;
	for (iter = list1.begin(); iter != list1.end(); ++iter)
		cout << *iter << " ";
	cout << endl;

	cout << "Insert 10 at end and display items from list : " ;
	list1.insert(list1.end(), 10);
	for (iter = list1.begin(); iter != list1.end(); ++iter)
		cout << *iter << " ";
	cout << endl;

	list < int >::reverse_iterator c1_rIter;
	cout << "Display items using reverse order : ";
	for (c1_rIter = list1.rbegin(); c1_rIter != list1.rend(); c1_rIter++)
		cout << *c1_rIter <<  " ";
	cout << endl;

	return 0;
}

						

Output

    					 
					Store items in list
					Display items from list: 0 1 2 3 4 5 6 7 8 9
					Insert 10 at end and display items from list: 0 1 2 3 4 5 6 7 8 9 10
					Display items using reverse order: 10 9 8 7 6 5 4 3 2 1 0
					    					 
					    

When to use

When you frequently add or remove items in the middle of your container, you can use list.

0 Comments



Please login to add comments.