Algorithm learning, algorithm Learning Website
Sorting function sort ()
This function is provided by STL and has powerful functions ~ Here we will teach you how to use it.
Sort () has three parameters: the first is the start position of sorting, the second is the end position of sorting, and the third is the judgment function of sorting. Function prototype:
sort(<#_RandomAccessIterator __first#>, <#_RandomAccessIterator __last#>, <#_Compare __comp#>)
This is the prototype ~
Usage
First, assume that we havevector<int> vec;Vector containers store a lot of unordered positive numbers, so we start to sort these integers with sort. First, the actual location is:vec.begin()The end position is:vec.end()The comparison function can be left empty. The default value is ascending. You can also write it manually.
Code Implementation
It is easy to directly view the code implementation ~
//// Main. cpp // hdu_1040 /// Created by Alps on 15/1/3. // Copyright (c) 2015 chen. all rights reserved. /// http://acm.hdu.edu.cn/showproblem.php? Pid = 1040 # include <iostream> # include <vector> using namespace std; bool sortRule (int a, int B) {return a <B;} int main (int argc, const char * argv []) {int n; scanf ("% d", & n); int num; int a = 0; for (int I = 0; I <n; I ++) {scanf ("% d", & num); vector <int> list; while (num --) {scanf ("% d ", & a); list. push_back (a);} sort (list. begin (), list. end (), sortRule); vector <int>: iterator iter; for (iter = list. beg In (); iter! = List. end (); iter ++) {printf ("% d", * iter); if (iter = list. end ()-1) {printf ("\ n") ;}else {printf ("") ;}} return 0 ;}
This is the implementation code ~ For test examples, see:
Http://acm.hdu.edu.cn/showproblem.php? The input format of pid = 1040.