C++

How to reverse a Vector in C++

If a vector has elements in the sequence, {‘A’, ‘B’, ‘C’, ‘D’, ‘E’} and it is reconfigured such that its sequence becomes, {‘E’, ‘D’, ‘C’, ‘B’, ‘A’} then the vector has been reversed. Unfortunately, such direct reversibility is not possible in C++. However, a vector in C++ can be iterated from the back, and that is indirect reversibility. With that, there is no need to reverse the vector literally. This article explains how to iterate a vector in C++ from the back and modify its elements.

Before using a vector in C++, the program should begin with,

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

with the vector library included. It is easier to understand reverse iteration after having a summary of forwarding iteration. So forward iteration is summarized first before the reverse iteration is explained.

Article Content

Forward Iteration

Forward iteration deals with two iterators. An iterator is an elaborated pointer object with special characteristics. Here, the two iterators of interest are returned by the begin() member function and the end() member function. The begin() member function returns an iterator that points to the first element of the vector. The end() member function returns an iterator that points just beyond the last element of the vector.

Assume that the name of the vector is vtr, then the following statement will return a begin iterator:

vector<char>::iterator p = vtr.begin();

where p is the name given to the begin iterator. The following statement will return an end iterator:

vector<char>::iterator q = vtr.end();

where q is the name given to the end iterator, it can be seen from the above two statements that p and q are of the same type and can even be interchanged.

All the code segments for this article are written in the main() function. The following code reads all the elements of the vector, from the beginning to the last element:

vector<char> vtr = {'A', 'B', 'C', 'D', 'E'};

for (vector<char>::iterator p = vtr.begin(); p != vtr.end(); p++) {
    cout << *p << ' ';
}
cout < vtr = {'A', 'B', 'C', 'D', 'E'};
   
vector<char>::iterator q = vtr.end();
for (q = --q; q >= vtr.begin(); q--) {
    cout << *q << ' ';
}
cout << endl;

The output is:

A B C D E

The code in the parentheses of the for-loop needs explanation. p is an iterator that first points to the first element of the vector. While it is not yet pointing just beyond the vector, it is incremented by p++ to point to each element in the vector. When it points to an element in the vector, the value (character) of the element is obtained with *p in the body of the for-loop. * is the indirection operator.

The following code reads and displays the values in the vector from the last element to the first element, using the end iterator:

vector<char>vtr = {'A', 'B', 'C', 'D', 'E'};
   
vector<char>::iterator q = vtr.end();
for (q = --q; q >= vtr.begin(); q--) {
   cout << *q << ' ';
}
cout << endl;

The output is:

 E D C B A

The end iterator points just beyond the end of the vector, and that is not an element. So, it has to be decremented first before it can point to the last element. From there, the iteration can go backward.

The while condition for the for-loop here is, “if q is greater than or equal to the begin iterator”. It cannot be “if q is not equal to the begin iterator,” as that would exclude the first element.

This is an informal way to iterate backward. That is, this is an informal way to reverse a vector indirectly.

Changing Value of an Element

When the instantiation of the vector is not preceded by const (for constant), the value of any element in the vector can be changed. The following code illustrates this:

vector<char> vtr = {'A', 'B', 'C', 'D', 'E'};
   
vector<char>::iterator q = vtr.end();
q--; q--; q--;
 
*q = 'Z';

vector<char>::iterator r = vtr.end();
for (r = --r; r >= vtr.begin(); r--) {
   cout << *r << ' ';
}
cout << endl;

The output is:

E D Z B A

The end iterator, q is decremented three times with “q–; q–; q–;” to point at ‘C’.

If the vector instantiation is preceded with const, then no element value can be changed. In this case, the constant forward iterator has to be returned for the end or begin iterator. The following code will not compile because an attempt is made to change the value of ‘C’:

const vector<char> vtr = {'A', 'B', 'C', 'D', 'E'};
   
vector<char>::const_iterator q = vtr.end();
q--; q--; q--;
 
*q = 'Z';

Reverse Iteration

Reverse iteration has two principal iterators. These iterators are returned by the member functions, rbegin() and rend(). rend() returns an iterator that points just in front of the first element of the vector. rbegin() returns an iterator that points to the last element of the vector. The following code reads and displays the elements of the vector, from the first to the last, in the forward direction:

vector<char> vtr = {'A', 'B', 'C', 'D', 'E'};

vector<char>>:reverse_iterator p = vtr.rend();
     
for (p = --p; p >= vtr.rbegin(); p--) {
    cout << *p << ' ';
}
cout << endl;

The output is:

A B C D E

The reverse iterator is used. Since rend() returns an iterator that points just in front of the first element, which is not an element, it has to be incremented to point to the first element. Since we are dealing with the reverse iterator, the increment operator here is — and not ++. Also, in the while condition, >= is used instead of <= .

The following code reads and displays the values in the vector, from the last element to the first element, using the iterator of rbegin():

vector<char> vtr = {'A', 'B', 'C', 'D', 'E'};
     
for (vector<char>::reverse_iterator q = vtr.rbegin(); q <= vtr.rend(); q++) {
   cout << *q << ' ';
}
cout << endl;

The output is:

E D C B A

The member function rbegin() returns an iterator that points to the last element of the vector. The iterator returned is a reverse_iterator. rend() returns an iterator that points just before the first element. Note that the while condition for the for-loop has but =, since we are dealing with a reverse iterator. Decrement with this iterator is ++ and not –.

Changing Value of an Element

When the instantiation of the vector is not preceded by const (for constant), the value of any element in the vector can be changed with the reverse_iterator. The following code illustrates this, with the reverse_iterator:

vector<char> vtr = {'A', 'B', 'C', 'D', 'E'};

vector<char>::reverse_iterator q = vtr.rbegin();
q++; q++;
 
*q = 'X';
     
for (vector<char>::reverse_iterator r = vtr.rbegin(); r <= vtr.rend(); r++) {
    cout << *r << ' ';
}
cout << endl;

The output is:

E D X B A

The rbegin() iterator, q is decremented two times with “q++; q++;” to point at ‘C’, since it initially points to the last element.

If the vector instantiation is preceded with const, then no element value can be changed, with an iterator, be it reverse_iterator iterator ( or forward). In this case, the constant reverse iterator has to be returned for the rbegin() or rend() function. The following code will not compile because an attempt is made to change the value of ‘C’:

const vector<char> vtr = {'A', 'B', 'C', 'D', 'E'};

vector<char>::const_reverse_iterator q = vtr.rbegin();
q++; q++;
 
*q = 'X';

Constant Reverse Iterator

crbegin() behaves like rbegin(), but returns a const_reverse_iterator, whether or not the instantiation of the vector was begun with const. This means the value of the iterator returned cannot be changed. crend() behaves like rend(), but returns a const_reverse_iterator, whether or not the instantiation of the vector was begun with const. This means the value of the iterator returned cannot be changed.

The following code displays all the values of the vector, using const_reverse_iterator, beginning from the last element:

vector<char> vtr = {'A', 'B', 'C', 'D', 'E'};
     
for (vector<char>::const_reverse_iterator q = vtr.crbegin(); q <= vtr.crend(); q++) {
    cout << *q << ' ';
}
cout << endl;

The output is:

E D C B A

The following code will not compile because we are dealing here with a constant reverse iterator. The instantiation of the vector is not preceded by const.

vector<char> vtr = {'A', 'B', 'C', 'D', 'E'};
     
for (vector<char>::reverse_iterator q = vtr.rbegin(); q <= vtr.rend(); q++) {
    cout << *q << ' ';
}
cout << endl;

Conclusion

A vector cannot be reversed literally. However, it can be iterated from back to front to have a similar result. With forward iteration, the member functions, begin() and end() are involved. In the case of reverse iteration, member functions, rbegin() and rend() are involved. In this case, the iterator involved is reverse_iterator and not an iterator. Still in this case, ++ is — and >= is <= . There is also const_reverse_iterator, for crbegin() and crend() member functions.

About the author

Chrysanthus Forcha

Discoverer of mathematics Integration from First Principles and related series. Master’s Degree in Technical Education, specializing in Electronics and Computer Software. BSc Electronics. I also have knowledge and experience at the Master’s level in Computing and Telecommunications. Out of 20,000 writers, I was the 37th best writer at devarticles.com. I have been working in these fields for more than 10 years.