sort function in cpp
C++
// A C++ program to demonstrate STL sort() using
// our own comparator
#include<bits/stdc++.h>
using namespace std;
// An interval has a 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()
{
Interval arr[] = { {6,8}, {1,9}, {2,4}, {4,7} };
int n = sizeof(arr)/sizeof(arr[0]);
// sort the intervals in increasing order of
// start time
sort(arr, arr+n, compareInterval);
cout << "Intervals sorted by start time : \n";
for (int i=0; i<n; i++)
cout << "[" << arr[i].start << "," << arr[i].end
<< "] ";
return 0;
}
// C++ program to demonstrate default behaviour of
// sort() in STL.
#include <bits/stdc++.h>
using namespace std;
int main()
{
int arr[] = {1, 5, 8, 9, 6, 7, 3, 4, 2, 0};
int n = sizeof(arr)/sizeof(arr[0]);
sort(arr, arr+n);
cout << "\nArray after sorting using "
"default sort is : \n";
for (int i = 0; i < n; ++i)
cout << arr[i] << " ";
return 0;
}
// C++ program to demonstrate descending order sort using
// greater<>().
#include <bits/stdc++.h>
using namespace std;
int main()
{
int arr[] = {1, 5, 8, 9, 6, 7, 3, 4, 2, 0};
int n = sizeof(arr)/sizeof(arr[0]);
sort(arr, arr+n, greater<int>());
cout << "Array after sorting : \n";
for (int i = 0; i < n; ++i)
cout << arr[i] << " ";
return 0;
}
Also in C++:
- Title
- built in function in c++ for binary to decimal
- Category
- C++
- Title
- advanced c++ topics
- Category
- C++
- Title
- how to find the number of cycles in a graph C++
- Category
- C++
- Title
- multiset c++
- Category
- C++
- Title
- c++ class member initialization
- Category
- C++
- Title
- how to use assrt c++
- Category
- C++
- Title
- index string c++
- Category
- C++
- Title
- properties of a set c++
- Category
- C++
- Title
- level order traversal
- Category
- C++
- Title
- c++ string^ to char*
- Category
- C++
- Title
- switch case sinax c++
- Category
- C++
- Title
- how to delete an element in vector pair in cpp
- Category
- C++
- Title
- 2927260.eps 2927262.jpg 2927263.ai License free.txt License premium.txt
- Category
- C++
- Title
- how to append two vectors in c++
- Category
- C++
- Title
- c++ transform
- Category
- C++
- Title
- calling by reference c++
- Category
- C++
- Title
- GetCurrentThreadId c
- Category
- C++
- Title
- c++ unittest in ros
- Category
- C++
- Title
- pause the console c++
- Category
- C++
- Title
- pass vector by reference c++
- Category
- C++
- Title
- create a 2d array c++
- Category
- C++
- Title
- mingw32/bin/ld.exe: C:\Users\mfrom\AppData\Local\Temp\ccSKcRks.o:PizzaPi.cpp:(.text$_ZN5PizzaC2Ev[__ZN5PizzaC2Ev]+0xa): undefined reference to `vtable for Pizza' collect2.exe: error: ld returned 1 exit status
- Category
- C++
- Title
- stringstream in c++ with delimiter
- Category
- C++
- Title
- find in set of pairs using first value cpp
- Category
- C++
- Title
- c++ show time elapsed
- Category
- C++
- Title
- create a dictionary cpp
- Category
- C++
- Title
- sorting of array in c++
- Category
- C++
- Title
- convert decimal to binary in c++
- Category
- C++
- Title
- nan c++ example
- Category
- C++
- Title
- struct c++
- Category
- C++
- Title
- how to take input in C++ in coding
- Category
- C++
- Title
- c++ ambigous error
- Category
- C++
- Title
- substr in c++
- Category
- C++
- Title
- c++ functions
- Category
- C++
- Title
- count function c++
- Category
- C++
- Title
- delete 2d dynamic array c++
- Category
- C++
- Title
- c++ reverse vector
- Category
- C++
- Title
- minimum swaps to sort an array
- Category
- C++
- Title
- how to find hcf in c++
- Category
- C++
- Title
- c++ triple
- Category
- C++
- Title
- initialize array c++
- Category
- C++
- Title
- string to vector c++
- Category
- C++
- Title
- sieve of eratosthenes c++
- Category
- C++
- Title
- c++ variable argument
- Category
- C++
- Title
- fail() in c++
- Category
- C++
- Title
- vector concat c++
- Category
- C++
- Title
- how to compile opencv c++ in ubuntu
- Category
- C++
- Title
- euler's totient function c++
- Category
- C++
- Title
- how to remove maximum number of characters in c++ cin,ignore
- Category
- C++
- Title
- how to ensure the user inouts a int and not anything else c++
- Category
- C++
- Title
- opencv compile c++
- Category
- C++
- Title
- what does count function do in hashmap
- Category
- C++
- Title
- how to append one vector to another c++
- Category
- C++
- Title
- passing a vector to a function c++
- Category
- C++
- Title
- loop through array c++
- Category
- C++
- Title
- sum of two numbers c++
- Category
- C++
- Title
- map.erase in c++
- Category
- C++
- Title
- how to iterate through array in c++
- Category
- C++
- Title
- declaration vs. definition cpp
- Category
- C++
- Title
- sfml base program
- Category
- C++
- Title
- c++ append to list
- Category
- C++
- Title
- pair in c++
- Category
- C++
- Title
- iterate through unordered_map c++ in reverse order
- Category
- C++
- Title
- c++ sort array of ints
- Category
- C++
- Title
- C++ Syntax
- Category
- C++
- Title
- how to open an input file in c++
- Category
- C++
- Title
- file objects in c++
- Category
- C++
- Title
- filling 2d array with 0 c++
- Category
- C++
- Title
- how initilaize deffult value to c++ class
- Category
- C++
- Title
- initialize int c++
- Category
- C++
- Title
- how to convert qt string to string
- Category
- C++
- Title
- vector stl c++
- Category
- C++
- Title
- appending a double to a string c++
- Category
- C++
- Title
- ceil c++;
- Category
- C++
- Title
- do while loop c++
- Category
- C++
- Title
- convert int to binary string c++
- Category
- C++
- Title
- translate
- Category
- C++
- Title
- c++ loop through array
- Category
- C++
- Title
- tellg and seekg c++
- Category
- C++
- Title
- return function in cpp
- Category
- C++
- Title
- linkedlist implementation in c++
- Category
- C++
- Title
- transpose matrix eigen c++
- Category
- C++
- Title
- bitset c++
- Category
- C++
- Title
- c++ iterate over vector
- Category
- C++
- Title
- input a string in c++
- Category
- C++
- Title
- cube mapping sdl
- Category
- C++
- Title
- preorder traversal
- Category
- C++
- Title
- simple timer arduino blynk library error
- Category
- C++
- Title
- passing array to function c++ pointer
- Category
- C++
- Title
- how to print nth palindrome number in c++
- Category
- C++
- Title
- find_if c++ example
- Category
- C++
- Title
- counting valleys hackerrank solution in c++
- Category
- C++
- Title
- c++ empty stream
- Category
- C++
- Title
- array 2d to 1d
- Category
- C++
- Title
- c++ parse int
- Category
- C++
- Title
- range based for loop c++ with reference
- Category
- C++
- Title
- what is difference between single inverted and double inverted in programming languages
- Category
- C++
- Title
- how to show c++ binary files in sublime text
- Category
- C++
- Title
- how to end a c++ program early
- Category
- C++
- Title
- uepic games github
- Category
- C++