STL演算法概述

來源:互聯網
上載者:User

#include<algorithm>    //C++標準庫演算法

#include<functional>   //仿函數和函數配接器

1.nonmodifying algorithm     非變動性演算法 (不改動元素,即只能讀取)

2.modifying algorithm           變動性演算法

3.removing algorithm            移除性演算法

4.mutating algorithm            變序性演算法

5.sorting algorithm               排序演算法

6,sorted range algorithm      已序區間演算法

7.numeric algorithm              數值演算法

 

difference_type用於測定距離,區別正負

1.非變動性演算法

(1) for_each(begin,end,Unary_func op)  具體見另外部落格

(2) difference_type count (begin,end,const T&value);   //返回value的個數

(3) difference_type count_if (begin,end,Unarypredicate op); //返回op為true的元素個數

(4) iterator min_element(begin,end);              //預設less than

     iterator min_element(begin,end,compFunc op);   //op(elem1,elem2);

(5) iterator max_element(begin,end);

     iterator max_element(begin,end,compFunc op);

(6) iterator find(begin,end,const T&value);

(7) iterator find_if(begin,end,UnaryPredicate op);

如果是已序區間用lower_bound(),upper_bound(),equal_range(),binary_search()可獲更高效能

(8) Input_iterator search_n(begin,end,Size count,const T& value);      //連續count個元素值全是value

     Input_iterator search_n(begin,end,Size count,const T&value,BinaryPredicate op);

     //連續count個元素造成op為true, op(elem,value)

(9) FowardIterator search(begin1,end1,begin2,end2); //在[begin1,end1)中尋找與[begin2,end2)相同的元素

      FowardIterator search(begin1,end1,Searchbegin2,Searchend2,BinaryPredicate op); 

      //op(elem,searchElem),

(10) Forward_Iterator find_end(begin1,end1,searchBegin2,searchEnd2);         

       //[begin1,end1)中最後出現的[searchBegin2,searchEnd2)

       Forward_Iterator find_end(begin1,end1,searchBegin2,searchEnd2,BinaryPredicate op);

       //op(elem,searchElem);

(11)Find_first_of 尋找與順序無關。

     Forward_Iterator  find_first_of(begin1,end1,searchBegin2,searchEnd2)

      //返回一個既在區間[begin1,end1)出現也在[searchBegin2,searchEnd2)出現的元素位置

     Forward_Iterator  find_first_of(begin1,end1,searchBegin2,searchEnd2,BinaryPredicate op);

      //返回一個區間[begin1,end1)和區間[searchBegin2,searchEnd2)內進行op(elem,searchElem)為true

(12) InputIterator adjacant_find(begin,end);                        //返回連續兩個相等元素的第一個位置

        InputIterator adjacant_find(begin,end,BinaryPredicate op);

        //op(elem,nextElem),連續兩個元素使以下二元判斷式的結果為true

(13) bool equal(begin1,end1,begin2);

        bool equal(begin1,end1,begin2,BinaryPredicate op);   //op(elem,cmpElem);

(14) pair<InputIterator1,InputIterator2> mismatch(InputIterator1 begin,InputIterator1 end,InputIterator2 cmpBeg)

        返回[begin,end)和cmpBeg開頭的區間第一組相異的對應元素

       pair<InputIterator1,InputIterator2> mismatch(InputIterator1 begin,InputIterator1 end,InputIterator2 cmpBeg,

                                                                                                                                                  BinaryPredicate op);

       op(elem,cmpElem); 返回使判斷式為false的對應元素

(15) bool lexicographical_compare(begin1,end1,begin2,end2);

        bool lexicographical_compare(begin1,end1,begin2,end2,ComFunc op);

        op(elem1,elem2);  elem1小於elem2則為true

 

字典順序定義:

1.數量不同,如果第一個序列數量少,則為true

2.相等則false

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.