Algorithm learning, algorithm Learning Website

Source: Internet
Author: User

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.

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.