CPP
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;
}
how to sort a vector in reverse c++
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> v = { 10, 9, 8, 6, 7, 2, 5, 1 };
sort(v.begin(), v.end(), greater <>());
}
how to sort a vector in descending order in c++
//Method 1
sort(v.begin(),v.end(),greater<>());
//Method 2
//Multiply the vector with -1 and then sort it and again multiply it with -1.
C++ reverse vector
// C++ program to reverse Vector
// using reverse() in STL
#include <bits/stdc++.h>
using namespace std;
int main()
{
// Get the vector
vector<int> a = { 1, 45, 54, 71, 76, 12 };
// Print the vector
cout << "Vector: ";
for (int i = 0; i < a.size(); i++)
cout << a[i] << " ";
cout << endl;
// Reverse the vector
reverse(a.begin(), a.end());
// Print the reversed vector
cout << "Reversed Vector: ";
for (int i = 0; i < a.size(); i++)
cout << a[i] << " ";
cout << endl;
return 0;
}
sort vector in reverse order c++
#include <iostream>
#include <vector>
#include <algorithm>
int main()
{
std::vector<int> vec = { 7, 3, 5, 1, 9 };
std::sort(vec.rbegin(), vec.rend());
// do anything
return 0;
}
reverse sort a vector
vector<int> v = { 10, 9, 8, 6, 7, 2, 5, 1 };
sort(v.begin(), v.end(), greater <>());
c++ reverse vector
#include <vector>
#include <algorithm>
int main() {
std::vector<int> a;
std::reverse(a.begin(), a.end());
return 0;
}
reverse in vector c++
reverse(start_index, last_index);