
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
How to sort a Vector in descending order using STL in C++?
In this tutorial, we will be discussing a program to understand how to sort a vector in descending order using STL in C++.
For sorting the given vector in descending order we will be using the sort() function from the STL library in C++.
Example
#include <bits/stdc++.h> using namespace std; int main(){ //collecting the vector vector<int> a = { 1, 45, 54, 71, 76, 12 }; cout << "Vector: "; for (int i = 0; i < a.size(); i++) cout << a[i] << " "; cout << endl; //sorting in descending order sort(a.begin(), a.end(), greater<int>()); cout << "Sorted Vector in descending order: "; for (int i = 0; i < a.size(); i++) cout << a[i] << " "; cout << endl; return 0; }
Output
Vector: 1 45 54 71 76 12 Sorted Vector in descending order: 76 71 54 45 12 1
- Related Articles
- Sort an array in descending order using C#
- Sorting a vector in descending order in C++
- How to sort List in descending order using Comparator in Java
- How to perform descending order sort in MongoDB?
- How to reverse a Vector using STL in C++?
- Sort list elements in descending order in C#
- How to sort TreeSet in descending order in Java?
- Golang Program to sort an array in descending order using insertion sort
- Swift Program to sort an array in descending order using bubble sort
- Swift Program to sort an array in descending order using selection sort
- C# program to sort an array in descending order
- C program to sort an array in descending order
- Descending order in Map and Multimap of C++ STL
- How do you sort an array in C# in descending order?
- How to sort an ArrayList in Java in descending order?

Advertisements