Ordered Collections

The bearing of a child takes nine months, no matter how many women are assigned. Many software tasks have this characteristic because of the sequential nature of debugging. Fred Brooks

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.