how to sort a vector in c++
C++
// C++ program to sort a vector in non-decreasing
// order.
#include <bits/stdc++.h> // Vector
#include <algorithm> // Sort
using namespace std;
int main()
{
// Initalizing the vector v with these values
vector<int> v{ 1, 5, 8, 9, 6, 7, 3, 4, 2, 0 };
// Vector is sorted in ascending order
sort(v.begin(), v.end());
return 0;
}
sort(v.begin(), v.end()); // A C++ program to sort vector using
// our own comparator
#include <bits/stdc++.h>
using namespace std;
// An interval has start time and end time
struct Interval {
int start, end;
};
// Compares two intervals according to staring times.
bool compareInterval(Interval i1, Interval i2)
{
return (i1.start < i2.start);
}
int main()
{
vector<Interval> v { { 6, 8 }, { 1, 9 }, { 2, 4 }, { 4, 7 } };
// sort the intervals in increasing order of
// start time
sort(v.begin(), v.end(), compareInterval);
cout << "Intervals sorted by start time : \n";
for (auto x : v)
cout << "[" << x.start << ", " << x.end << "] ";
return 0;
}
#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 <>());
}// C++ program to sort a vector in non-decreasing
// order.
#include <algorithm> //Sort
#include <bits/stdc++.h>
using namespace std;
int main()
{
vector<int> v{ 1, 5, 8, 9, 6, 7, 3, 4, 2, 0 };
sort(v.begin(), v.end());
return 0;
}
Also in C++:
- Title
- function template
- Category
- C++
- Title
- c++ wait for user input
- Category
- C++
- Title
- static variable in c++
- Category
- C++
- Title
- c++ char to int
- Category
- C++
- Title
- arduino falling edge
- Category
- C++
- Title
- how to use max_element in c++ with vector
- Category
- C++
- Title
- how to use assrt c++
- Category
- C++
- Title
- compile c++ linux
- Category
- C++
- Title
- how to measure program run time in c++
- Category
- C++
- Title
- get data from terminal c++
- Category
- C++
- Title
- how to ensure the user inouts a int and not anything else c++
- Category
- C++
- Title
- insert elements in array in c++11
- Category
- C++
- Title
- c++ program for addition of two numbers using functions
- Category
- C++
- Title
- mkdir c++
- Category
- C++
- Title
- multiset c++
- Category
- C++
- Title
- c++ iterate through constant list
- Category
- C++
- Title
- c++ server service ros
- Category
- C++
- Title
- Temporary file using MSFT API in cpp
- Category
- C++
- Title
- c++ print one line to console instead of multiple
- Category
- C++
- Title
- residuo en lenguaje c
- Category
- C++
- Title
- c++ transform
- Category
- C++
- Title
- type id c++
- Category
- C++
- Title
- file objects in c++
- Category
- C++
- Title
- c++ std::copy to cout
- Category
- C++
- Title
- c++ find object in vector by attribute
- Category
- C++
- Title
- c++ triple
- Category
- C++
- Title
- C++ string format ctime
- Category
- C++
- Title
- length of array in cpp
- Category
- C++
- Title
- how are graphics in games made
- Category
- C++
- Title
- c++ function to find length of array
- Category
- C++
- Title
- set of vectors c++
- Category
- C++
- Title
- c++ functions
- Category
- C++
- Title
- initialize 3d vector c++
- Category
- C++
- Title
- char **
- Category
- C++
- Title
- clear qlayout
- Category
- C++
- Title
- new in c++
- Category
- C++
- Title
- coronavirus
- Category
- C++
- Title
- what is order in of preeendence in float, int, char, bool
- Category
- C++
- Title
- c++ char define
- Category
- C++
- Title
- pairs in c++
- Category
- C++
- Title
- run program until ctrl-d c++
- Category
- C++
- Title
- substr c++
- Category
- C++
- Title
- placement new c++
- Category
- C++
- Title
- queue c++
- Category
- C++
- Title
- variabvles in c++
- Category
- C++
- Title
- binary search stl in c++
- Category
- C++
- Title
- write to file in C++
- Category
- C++
- Title
- create copy of range of string c++
- Category
- C++
- Title
- uepic games github
- Category
- C++
- Title
- how to round to nearest whole number unity
- Category
- C++
- Title
- new class * [] c++
- Category
- C++
- Title
- c++ excel cell blank cells
- Category
- C++
- Title
- modular exponentiation c++
- Category
- C++
- Title
- Combination Sum
- Category
- C++
- Title
- swapo algorit
- Category
- C++
- Title
- initialize array c++
- Category
- C++
- Title
- Insert into vector C++
- Category
- C++
- Title
- initialize int c++
- Category
- C++
- Title
- how to iterate through array in c++
- Category
- C++
- Title
- check for bst
- Category
- C++
- Title
- reverse a linked list using recursion
- Category
- C++
- Title
- how to sort a vector in reverse c++
- Category
- C++
- Title
- how read a shader from another file c++
- Category
- C++
- Title
- remove value from vector c++
- Category
- C++
- Title
- C++ Student::Student()
- Category
- C++
- Title
- cloud hosting
- Category
- C++
- Title
- ue4 c++ overlapping functions cpp setup
- Category
- C++
- Title
- lisy stl C++
- Category
- C++
- Title
- fill c++
- Category
- C++
- Title
- how to have a queue as a parameter in c++
- Category
- C++
- Title
- how to sort a vector in c++
- Category
- C++
- Title
- how can make string value in cpp
- Category
- C++
- Title
- c++ typeid get type name
- Category
- C++
- Title
- command line options in c++
- Category
- C++
- Title
- c++ main function
- Category
- C++
- Title
- double max value c++
- Category
- C++
- Title
- create a 2d array c++
- Category
- C++
- Title
- lopping over an array c++
- Category
- C++
- Title
- factorion
- Category
- C++
- Title
- check if key exists in map c++
- Category
- C++
- Title
- self in c++
- Category
- C++
- Title
- iterative inorder traversal
- Category
- C++
- Title
- c++ read file line by line
- Category
- C++
- Title
- cpp nan value
- Category
- C++
- Title
- findung the mode in c++
- Category
- C++
- Title
- how to iterate trough a vector in c++
- Category
- C++
- Title
- how to get a letter from the user c++ string
- Category
- C++
- Title
- least number of coins to form a sum
- Category
- C++
- Title
- how initilaize deffult value to c++ class
- Category
- C++
- Title
- C++ int to char*
- Category
- C++
- Title
- c++ overload operator
- Category
- C++
- Title
- range of int
- Category
- C++
- Title
- accumulate in cpp
- Category
- C++
- Title
- how to get a letter from the users string in c++
- Category
- C++
- Title
- visual studio 2019 read and write text file c++
- Category
- C++
- Title
- what is iterator in c++?
- Category
- C++
- Title
- runtime error in c++
- Category
- C++
- Title
- how to define a while statement in c++
- Category
- C++
- Title
- delete a double pointer c++
- Category
- C++
- Title
- SFML window
- Category
- C++