關於C++中的一些函數

來源:互聯網
上載者:User

標籤:

最近寫代碼需要用到排序函數,自己水平有限,故使用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]

 

關於C++中的一些函數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.