Reverse a vector in C++

In C++, reversing a vector is a common task that can be achieved in various ways. A vector is a dynamic array that can shrink or grow in size as elements are removed or added. This blog post will explore different methods to reverse a vector in C++. We will also provide a step-by-step guide on how to implement various methods.

Method 1: Using the reverse Algorithm:

One of the simplest ways to reverse a vector is by using the reverse algorithm provided by the <algorithm> library in C++. The reverse algorithm takes two iterators as input, which specify the range of elements to be reversed. Here’s an example code snippet that demonstrates how to use the reverse algorithm:

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;
int main() {
    vector<int> vect = {4, 5, 6, 7, 8};

    reverse(vect.begin(), vect.end());  //for reverse a vector

    //for printing the reversed vector 
    for (int i : vect) {
        cout << i << " ";
    }

    return 0;
}

This code will reverse the elements of the vect vector, resulting in the output {8, 7, 6, 5, 4}.

More on reverse() function

Method 2: Using Two Pointers:

Another way to reverse a vector is by using two pointers, one at the beginning and one at the end of the vector. Swap the elements at these positions and move the pointers towards each other until they meet. Here’s an example code snippet that demonstrates this method:

#include <iostream>
#include <vector>

using namespace std;

int main() {
    vector<int> vect = {4, 5, 6, 7, 8};
    int left = 0;
    int right = vect.size() - 1;
    
    while (left < right) {
        swap(vect[left], vect[right]);
        left++;
        right--;
    }
    
    for (int i : vect) {
        cout << i << " ";
    }

    return 0;
}

This code also reverses the elements of the vect vector, resulting in the same output as the previous example.

Method 3: Using Iterators:

We can also use iterators to reverse a vector. This method involves creating two iterators, one at the beginning and one at the end of the vector, and then swapping the elements at these positions using a loop. Here’s an example code snippet that demonstrates how to use this method:

#include <iostream>
#include <vector>

using namespace std;

int main() {
    vector<int> vect = {4, 5, 6, 7, 8};
    vector<int>::iterator it = vect.begin();
    vector<int>::iterator it_end = vect.end();

    while (it < it_end) {
        --it_end;
        if (it == it_end) {
            break;
        }
        swap(*it, *it_end);
        ++it;
    }

    // displaying the reversed vector
    for (int i = 0; i < vect.size(); i++) {
        cout << vect[i] << " ";
    }
    cout << std::endl;

    return 0;
}

This code will also reverse the elements of the vect vector, resulting in the same output as the previous examples.

In conclusion, there are several ways to reverse a vector in C++. Each method has its own advantages and disadvantages, and choosing the right method depends on the specific requirements of your application. By using one of these methods, you can easily reverse a vector and achieve your desired output.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top