Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: C++
Expertise: Intermediate
Jan 24, 2005

The reverse() Algorithm

You can use this algorithm to reverse the order of elements in any sequence container. It takes two bidirectional iterators as argument:

	int iArr [] = {1, 2, 3, 4, 6, 7};

	#include <algorithm> //for reverse
	reverse(iArr, iArr+7); //iArr has now the reversed sequence 7, 6, 4, 3, 2, 1
As you see, this algorithm changes the original container. However, should you want the original container unchanged or to store the reversed order of an element at some other location (or in any other container), use reverse_copy() algorithm.

reverse_copy() takes two bidirectional iterators each of which indicates the beginning and the end of the first container whose contents are to be reversed. It also takes one output iterator indicating the start position of the container slated to store the reversed order of elements.


	vector<int> vecInt;

	reverse_copy(iArr, iArr+7, back_inserter(vecInt));
Saurabh Ramya
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap