sort function in c++
C++
sort(arr, arr+n); // sorts in ascending order#include <iostream>
2 #include <array>
3 #include <string>
4 #include <algorithm>
5
6 using namespace std;
7
8 int main(){
9 array<string, 4> colours = {"blue", "black", "red", "green"};
10 for (string colour : colours){
11 cout << colour << ' ';
12 }
13 cout << endl;
14 sort(colours.begin(), colours.end());
15 for (string colour : colours){
16 cout << colour << ' ';
17 }
18 return 0;
19 }
66
20
21 /*
22 Output:
23 blue black red green
24 black blue green red
25 */ int arr[]= {2,3,5,6,1,2,3,6,10,100,200,0,-10};
int n = sizeof(arr)/sizeof(int);
sort(arr,arr+n);
for(int i: arr)
{
cout << i << " ";
}
// 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;
}
// 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;
}
// 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;
}
Also in C++:
- Title
- c++ initialise array
- Category
- C++
- Title
- c++ excel cell blank cells
- Category
- C++
- Title
- c++ class constructor
- Category
- C++
- Title
- arrow operator c++
- Category
- C++
- Title
- c++ code for polynomial addition
- Category
- C++
- Title
- to_string c++
- Category
- C++
- Title
- std::substring
- Category
- C++
- Title
- sleep system function linux c++
- Category
- C++
- Title
- c++ create array
- Category
- C++
- Title
- vector stl c++
- Category
- C++
- Title
- maximum possible number atmost k swaps
- Category
- C++
- Title
- use of strstr in c++
- Category
- C++
- Title
- binary serach in c++
- Category
- C++
- Title
- min heap declaration in c++ stl
- Category
- C++
- Title
- iterar un map c++
- Category
- C++
- Title
- c++ map insert
- Category
- C++
- Title
- c++ give options string
- Category
- C++
- Title
- how to get os name in c++
- Category
- C++
- Title
- array 2d to 1d
- Category
- C++
- Title
- how to know the correct class of objects cpp
- Category
- C++
- Title
- how to modulo 10^9+7
- Category
- C++
- Title
- what are the different ways to traverse a binary tree
- Category
- C++
- Title
- add a timer c++
- Category
- C++
- Title
- initialize array c++
- Category
- C++
- Title
- c++ class method example
- Category
- C++
- Title
- c++ multidimensional vector
- Category
- C++
- Title
- COunt the number of continous subsequences such that the sum is between
- Category
- C++
- Title
- file format not recognized treating as linker script c++
- Category
- C++
- Title
- run program until ctrl-d c++
- Category
- C++
- Title
- c++ random
- Category
- C++
- Title
- heap sort
- Category
- C++
- Title
- using namespace std in c++
- Category
- C++
- Title
- widechartomultibyte
- Category
- C++
- Title
- binary search stl in c++
- Category
- C++
- Title
- how to sort in descending order c++
- Category
- C++
- Title
- how to append two vectors in c++
- Category
- C++
- Title
- formal parameter c++
- Category
- C++
- Title
- reference function in c++
- Category
- C++
- Title
- ceil in c++
- Category
- C++
- Title
- c++ bubble sort array
- Category
- C++
- Title
- linkedlist implementation in c++
- Category
- C++
- Title
- c++ pause
- Category
- C++
- Title
- c++ push multiple elements to vector
- Category
- C++
- Title
- double to string c++
- Category
- C++
- Title
- error: ‘memset’ was not declared in this scope in cpp
- Category
- C++
- Title
- iterate const vector
- Category
- C++
- Title
- how to append one vector to another c++
- Category
- C++
- Title
- calling by reference and pointers c++
- Category
- C++
- Title
- passing a vector to a function c++
- Category
- C++
- Title
- stoi c++
- Category
- C++
- Title
- c++ string to stream
- Category
- C++
- Title
- nginx linux
- Category
- C++
- Title
- c++ convert const char* to LPCWSTR
- Category
- C++
- Title
- string input
- Category
- C++
- Title
- pbds in c++
- Category
- C++
- Title
- what is time complexity of swap function
- Category
- C++
- Title
- how to include everything in c++
- Category
- C++
- Title
- how to pass an object by reference in c++
- Category
- C++
- Title
- error: invalid conversion from 'Node*' to 'int'
- Category
- C++
- Title
- c++ string
- Category
- C++
- Title
- check if intent has extras
- Category
- C++
- Title
- how to make a heap using stl in c++
- Category
- C++
- Title
- comparing strings c++
- Category
- C++
- Title
- how to print for limited decimal values in c++
- Category
- C++
- Title
- clear qlayout
- Category
- C++
- Title
- c++ calculator program using switch case
- Category
- C++
- Title
- phph date
- Category
- C++
- Title
- regexp_like oracle c++
- Category
- C++
- Title
- sum of vector c++
- Category
- C++
- Title
- clear console c++
- Category
- C++
- Title
- c++ vector constructors
- Category
- C++
- Title
- c++ lettura file
- Category
- C++
- Title
- c++ yes no question
- Category
- C++
- Title
- c++ assert
- Category
- C++
- Title
- friend function in c++
- Category
- C++
- Title
- namespaces c++
- Category
- C++
- Title
- How to find the kth smallest number in cinstant space
- Category
- C++
- Title
- initialising 2d vector
- Category
- C++
- Title
- matrix eigen c++ example
- Category
- C++
- Title
- Combination Sum
- Category
- C++
- Title
- create a bitset of 1024 bits,
- Category
- C++
- Title
- removing repeated characters in a string c++
- Category
- C++
- Title
- how to put a class in a .h file c++
- Category
- C++
- Title
- c++ pi
- Category
- C++
- Title
- check if key exists in map c++
- Category
- C++
- Title
- append string to another string c++
- Category
- C++
- Title
- is x prime?
- Category
- C++
- Title
- call by reference c++ example
- Category
- C++
- Title
- sum of integer in array c++
- Category
- C++
- Title
- c++ reset stream
- Category
- C++
- Title
- 1d fixed length arrays c++
- Category
- C++
- Title
- how can make string value in cpp
- Category
- C++
- Title
- find in set of pairs using first value cpp
- Category
- C++
- Title
- how to find the mode of a vector c++
- Category
- C++
- Title
- c++ sort
- Category
- C++
- Title
- how to find length of string in c++
- Category
- C++
- Title
- length of 2d array c++
- Category
- C++
- Title
- how to output to console c++
- Category
- C++
- Title
- c++ tutorial
- Category
- C++
- Title
- create a 2d array c++
- Category
- C++