0

Below the C++ code is supposed to reverse the vector.

#include <bits/stdc++.h>

using namespace std;

vector<int> reverseArray(vector<int> a){
    vector<int> b;
    for (int i=0;i<a.size();i++){
        b.at(i) = a.at(a.size()-i-1);
    }
    return b;
}

int main(){
    int input;
    vector<int> arr;

    // read vector
    while(cin >> input){
        arr.push_back(input);
    }

    // print vector
    for (int i=0;i<arr.size();i++){
        cout << arr.at(i) << " ";
    }
    cout << endl <<"Reversed vector is:- ";
    vector<int> r_arr = reverseArray(arr);

    // print reversed vector
    for (int i=0;i<r_arr.size();i++){
        cout << arr.at(i) << " ";
    }
}

However, an error is being thrown as follows:-

terminate called after throwing an instance of 'std::out_of_range'
  what():  vector::_M_range_check: __n (which is 0) >= this->size() (which is 0)
Aborted (core dumped)

I checked that a.size()-i-1 is varying from 5 to 0 for a.size() = 6. Why is the code not working then? Where is the problem?

Please help, I'm learning C++ STL.

2 Answers 2

4

std::vector<int> b uses the default constructor (1) of std::vector which initializes the vector with a size of 0 so b.at(i) will always be out of range.

You need to initialize it with the same size as a using std::vector<int> b(a.size()).

But reversing a container is much easier using the reverse iterators (rbegin(), rend()):

std::vector<int> reverseArray(vector<int> a){
    std::vector<int> b(a.rbegin(), a.rend());
    return b;
}

so you actually don't need a function for that:

cout << endl << "Reversed vector is:- ";
std::vector<int> r_arr(arr.rbegin(), arr.rend());

And if it is only about printing/iterating it in revers order, then you don't even need an intermediate vector:

// print vector reversed
for (auto it=arr.rbegin(); it!=arr.rend(); it++){
  cout << (*it) << " ";
}

And if you want to reverse an existing container in place and you don't need the original order you can use std::reverse

std::reverse(std::begin(arr), std::end(arr));
for( auto i: arr ) {
  cout << i << " ";
}

Except for learning purposes, there is absolutely no reason to manually reverse an array with a loop.

Sign up to request clarification or add additional context in comments.

2 Comments

@user9194161 what do you mean with that? In your code? in my code?
Sorry! It was in my code. It is working correctly. Thank you sir.
2

You need to ensure that b is the same size as a before you start arbitrarily assigning to it. Just an extra constructor parameter should do it:

vector<int> reverseArray(vector<int> a){
    vector<int> b(a.size());
    for (int i=0;i<a.size();i++){
        b.at(i) = a.at(a.size()-i-1);
    }
    return b;
}

Also, your final loop for printing out the reversed array, is indexing the wrong array in the cout. It should read:

// print reversed vector
for (int i=0;i<r_arr.size();i++){
    cout << r_arr.at(i) << " ";
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.