zoukankan      html  css  js  c++  java
  • 关于C++中的一些函数

    最近写代码需要用到排序函数,自己水平有限,故使用C++自带的库函数。

    sort()定义 :

    template<class RanIt>
    void sort (RanIt first, RanIt last);


    template<class RanIt, class Pred>
    void sort (RanIt first, RanIt last, Pred pr);


    The first template function reorders the sequence designated by iterators in the
    range [first, last) to form a sequence ordered by (page 39) operator< . Thus, the
    elements are sorted in ascending order.

    reference:Standard  C++   Library  Reference  270页下。

    第一个模板函数对已知的序列进行排序,其中排序的范围是 [first , last),默认的排序方式是升序。

    在这里对范围进行一个说明。因为在网上查关于sort的信息是很多网友只是说范围中第一个参数是排序数据起始地址,

    后一个是排序数据的结束地址,这里有点不太好理解。更具体的说法是:first指向需要排序的数据的起始单元,last指向

    需要排序的数据的最后一个数据的下一个数据,这个数据不参与排序。last是不包含在排序范围内的(左闭右开)。

    具体而言就是:

    int  data[M] = ***;

    sort(data , data + M);//对data[M]中的所有数据进行排序

    或者sort(&data[0] , &data[M]);//注意数组data[M]的最后一个数据是data[M - 1],data[M]并不属于data[M]

     

  • 相关阅读:
    73. Set Matrix Zeroes
    289. Game of Live
    212. Word Search II
    79. Word Search
    142. Linked List Cycle II
    141. Linked List Cycle
    287. Find the Duplicate Number
    260. Single Number III
    137. Single Number II
    Oracle EBS中有关Form的触发器的执行顺序
  • 原文地址:https://www.cnblogs.com/jiahu-Blog/p/4433897.html
Copyright © 2011-2022 走看看