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. C.A.R. Hoare

forward_list

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

0 Comments


Definition

The forward_list is a container that implement on single linked list. The singly linked list can be iterated only forward.

Character

forward_list objects are single-linked lists.
It iterate the elements only forward.
Cannot access the items using index.

source Code

								
#include "stdafx.h"

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

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

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

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

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


	list1.reverse();
	cout << "Reverse the list items and display from list : ";
	for (iter = list1.begin(); iter != list1.end(); ++iter)
		cout << *iter << " ";
	cout << endl;
	

	return 0;
}

						

Output

    					 
					Store items in list
					Display items from list: 9 8 7 6 5 4 3 2 1 0
					Reverse the list items and display from list: 0 1 2 3 4 5 6 7 8 9
					    					 
					    

When to use

When you need fast insertion and removal, you can use forward_list.

0 Comments



Please login to add comments.