1sort(arr, arr+length); //increase
2sort(arr, arr+length, greater<int>()); //decrease
1 int arr[]= {2,3,5,6,1,2,3,6,10,100,200,0,-10};
2 int n = sizeof(arr)/sizeof(int);
3 sort(arr,arr+n);
4
5 for(int i: arr)
6 {
7 cout << i << " ";
8 }
9
1#include<bits/stdc++.h>
2
3vector<int> v = { 6,1,4,5,2,3,0};
4sort(v.begin() , v.end()); // {0,1,2,3,4,5,6} sorts ascending
5sort(v.begin(), v.end(), greater<int>()); // {6,5,4,3,2,1,0} sorts descending
1#include <algorithm> // std::sort
2
3int myints[] = {32,71,12,45,26,80,53,33};
4// using default comparison (operator <):
5std::sort (myvector.begin(), myvector.begin()+4); //(12 32 45 71)26 80 53 33
6
7// fun returns some form of a<b
8std::sort (myvector.begin()+4, myvector.end(), myfunction); // 12 32 45 71(26 33 53 80)
1// sort algorithm example
2#include <iostream> // std::cout
3#include <algorithm> // std::sort
4#include <vector> // std::vector
5
6bool myfunction (int i,int j) { return (i<j); }
7
8struct myclass {
9 bool operator() (int i,int j) { return (i<j);}
10} myobject;
11
12int main () {
13 int myints[] = {32,71,12,45,26,80,53,33};
14 std::vector<int> myvector (myints, myints+8); // 32 71 12 45 26 80 53 33
15
16 // using default comparison (operator <):
17 std::sort (myvector.begin(), myvector.begin()+4); //(12 32 45 71)26 80 53 33
18
19 // using function as comp
20 std::sort (myvector.begin()+4, myvector.end(), myfunction); // 12 32 45 71(26 33 53 80)
21
22 // using object as comp
23 std::sort (myvector.begin(), myvector.end(), myobject); //(12 26 32 33 45 53 71 80)
24
25 // print out content:
26 std::cout << "myvector contains:";
27 for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
28 std::cout << ' ' << *it;
29 std::cout << '\n';
30
31 return 0;
32}