array.reverse c++ code example
Example 1: reverse an array in c++
#include <iostream>
using namespace std;
int main()
{
const int SIZE = 9;
int arr [SIZE];
cout << "Enter numbers: \n";
for (int i = 0; i < SIZE; i++)
cin >> arr[i];
for (int i = 0; i < SIZE; i++)
cout << arr[i] << "\t";
cout << endl;
cout << "Reversed Array:\n";
int end = SIZE - 1, temp;
for (int i = 0; i < end; i++)
{
temp = arr[i];
arr[i] = arr[end];
arr[end] = temp;
end--;
}
for (int i = 0; i < SIZE; i++)
cout << arr[i] << "\t";
cout << endl;
}
Example 2: reverse() in c++
#include <iostream>
#include <algorithm>
#include <vector>
#include <iterator>
using namespace std;
int main()
{
vector<int>a = {11,22,33,44,99,55};
reverse(a.begin(), a.end());
auto it = a.begin();
for(it= a.begin(); it!=a.end(); it++){
cout << *it << ' ';
}
}