devxlogo

The reverse() Algorithm

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  //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 vecInt;	reverse_copy(iArr, iArr+7, back_inserter(vecInt));
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist