Katie Stevers
Katie Stevers

Reputation: 145

reversing a vector and printing it

How do you reverse a vector? I've read a lot of online post but I can't find one using namespace std. I need to use reverse() and vect.reverse(); Here is my code:

#include <iostream> 
#include <vector>
#include <iomanip>

using namespace std;
int main()
{
   cout << "Kaitlin Stevers" << endl;
   cout << "Exercise 11 - Vectors" << endl;
   cout << "November 12, 2016" <<endl;
   cout << endl;
   cout << endl;
   int size;
   cout << " How many numbers would you like the vector to hold? " << endl;
   cin >> size;
   vector<int> numbers;
   int bnumbers;

   for (int count = 0; count < size; count++)
   {
       cout << "Enter a number: " << endl;
       cin >> bnumbers;
       numbers.push_back(bnumbers); // Adds an element to numbers
    }
    //display the numbers stored in order
    cout << "The numbers in order are: " << endl;
    for(int bcount = 0; bcount < size; bcount++)
    {
        cout << numbers[bcount] << " ";
    }
    cout << endl;
    //display the numbers stored reversed
    reverse(numbers.begin(), numbers.end());
   return 0;
}

Upvotes: 0

Views: 55

Answers (1)

Trevor Hickey
Trevor Hickey

Reputation: 37816

You need to include <algorithm> to have access to std::reverse.
The last line in your code will work as is, if you include the header.

Upvotes: 3

Related Questions