Algorithm Learning-STL's P-sort function (sort) uses

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.