Ordered Collections

Its hard enough to find an error in your code when you are looking for it; its even harder when you ve assumed your code is error-free. Steve McConnell

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.