Sort function Sort ()
This function is the STL comes with, the function is very powerful ~ here teaches the use method.
Sort () has three parameters, the first is the starting position of the sort, the second is the end position of the sort, and the third is the judging function of the sort. The function prototypes are:
sort(<#_RandomAccessIterator __first#>, <#_RandomAccessIterator __last#>, <#_Compare __comp#>)
This is the prototype.
How to use
First, let's say we have a vector<int> vec; vector container that holds a lot of unordered positives, so we're going to sort these integers with sort. First, the position is: vec.begin() the end position is: vec.end() , the comparison function can not write, the default is ascending. can also be handwritten.
Code implementation
Just looking at the code implementation will be very simple ~
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.begin (); ITER! = List.end (); iter++) {printf ("%d", *iter); if (iter = = List.end ()-1) {printf ("\ n"); }else{printf (""); }}} return 0;}
This is an implementation code ~ Test example SEE:
The input format given by the http://acm.hdu.edu.cn/showproblem.php?pid=1040.
Algorithm Learning-STL's P-sort function (sort) uses