Answers for "reverse() in c++"

C++
21

c++ reverse vector

#include <bits/stdc++.h> // Vector
#include <algorithm>  // Reverse 
using namespace std;

int main()
{
    vector<int> nums{4,1,2,1,2};

    reverse(nums.begin(), nums.end());
    return 0;
}
Posted by: Guest on April-26-2020
2

reverse an array in c++

#include <iostream>
using namespace std;
int main()
{		// While loop
	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 temp, start = 0, end = SIZE-1;
	while (start < end)
	{
		temp = arr[start];
		arr[start] = arr[end];
		arr[end] = temp;
		start++;
		end--;
	}
	for (int i = 0; i < SIZE; i++)
		cout << arr[i] << "\t";
	cout << endl;	
  	return 0;
}
Posted by: Guest on January-12-2021
1

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 << ' ';    
    }
}
Posted by: Guest on March-05-2021
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--;
	}
	/*	Reverse using while loop
	int temp, start = 0, end = SIZE-1;
	while (start < end)
	{
		temp = arr[start];
		arr[start] = arr[end];
		arr[end] = temp;
		start++;
		end--;
	}*/
	for (int i = 0; i < SIZE; i++)
		cout << arr[i] << "\t";
	cout << endl;	
	}
Posted by: Guest on January-12-2021
1

c++ reverse part of vector

//Reverse vector partially (from index x to index y)
reverse(v.begin()+x, v.begin()+y+1);
Posted by: Guest on October-05-2020
2

reverse a vector

vector<int> a = {1,2,3,4,5,6};
reverse(a.begin(), a.end());
Posted by: Guest on July-02-2020

Browse Popular Code Answers by Language