日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

STL中算法锦集(三)

發布時間:2024/4/11 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 STL中算法锦集(三) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

STL中算法錦集(三)

文章目錄

  • STL中算法錦集(三)
    • 一、< algorithm >
      • 1.std::find_if
      • 2.std::find_if_not
      • 3.std::for_each
      • 4.std::generate
      • 5.std::generate_n
      • 6.std::includes
      • 7.std::inplace_merge
      • 8.std::is_heap
      • 9.std::is_heap_until
      • 10.std::is_partitioned

一、< algorithm >

1.std::find_if

  • 原型:
template <class InputIterator, class UnaryPredicate>InputIterator find_if (InputIterator first, InputIterator last, UnaryPredicate pred);
  • 參數:
  • InputIterator first:區間的開始
  • InputIterator last:區間的結束
  • UnaryPredicate pred:自定義比較規則,可以是函數指針或者仿函數對象、lambda表達式
  • 作用:[first,last)區間內找滿足pred條件的元素
  • 返回值:返回第一個滿足pred條件的位置
  • 實現:
template<class InputIterator, class UnaryPredicate>InputIterator find_if (InputIterator first, InputIterator last, UnaryPredicate pred) {while (first!=last) {if (pred(*first)) return first;++first;}return last; }
  • 案例:
// find_if example #include <iostream> // std::cout #include <algorithm> // std::find_if #include <vector> // std::vectorbool IsOdd (int i) {return ((i%2)==1); }int main () {std::vector<int> myvector;myvector.push_back(10);myvector.push_back(25);myvector.push_back(40);myvector.push_back(55);std::vector<int>::iterator it = std::find_if (myvector.begin(), myvector.end(), IsOdd);std::cout << "The first odd value is " << *it << '\n';return 0; }Output: The first odd value is 25

2.std::find_if_not

  • 原型:
template <class InputIterator, class UnaryPredicate>InputIterator find_if_not (InputIterator first, InputIterator last, UnaryPredicate pred);
  • 參數:
  • InputIterator first:區間的開始
  • InputIterator last:區間的結束
  • UnaryPredicate pred:自定義比較規則,可以是函數指針或者仿函數對象、lambda表達式
  • 作用:[first,last)區間找滿足第一個不滿足pred條件元素的位置
  • 返回值:返回第一個不滿足pred條件元素的位置
  • 實現:
template<class InputIterator, class UnaryPredicate>InputIterator find_if_not (InputIterator first, InputIterator last, UnaryPredicate pred) {while (first!=last) {if (!pred(*first)) return first;++first;}return last; }
  • 案例:
// find_if_not example #include <iostream> // std::cout #include <algorithm> // std::find_if_not #include <array> // std::arrayint main () {std::array<int,5> foo = {1,2,3,4,5};std::array<int,5>::iterator it =std::find_if_not (foo.begin(), foo.end(), [](int i){return i%2;} );std::cout << "The first even value is " << *it << '\n';return 0; }Output: The first even value is 2

3.std::for_each

  • 原型:
template <class InputIterator, class Function>Function for_each (InputIterator first, InputIterator last, Function fn);
  • 參數:
  • InputIterator first:區間的開始
  • InputIterator last:區間的結束
  • Function fn:自定義動作,可以是函數指針或者仿函數對象、lambda表達式
  • 作用:對[first,last)區間內的每個元素執行fn
  • 返回值:返回每個元素執行的函數fn
  • 實現:
template<class InputIterator, class Function>Function for_each(InputIterator first, InputIterator last, Function fn) {while (first!=last) {fn (*first);++first;}return fn; // or, since C++11: return move(fn); }
  • 案例:
// for_each example #include <iostream> // std::cout #include <algorithm> // std::for_each #include <vector> // std::vectorvoid myfunction (int i) { // function:std::cout << ' ' << i; }struct myclass { // function object type:void operator() (int i) {std::cout << ' ' << i;} } myobject;int main () {std::vector<int> myvector;myvector.push_back(10);myvector.push_back(20);myvector.push_back(30);std::cout << "myvector contains:";for_each (myvector.begin(), myvector.end(), myfunction);std::cout << '\n';// or:std::cout << "myvector contains:";for_each (myvector.begin(), myvector.end(), myobject);std::cout << '\n';return 0; }Output: myvector contains: 10 20 30 myvector contains: 10 20 30

4.std::generate

  • 原型:
template <class ForwardIterator, class Generator>void generate (ForwardIterator first, ForwardIterator last, Generator gen);
  • 參數:
  • ForwardIterator first:區間的開始
  • ForwardIterator last:區間的結束
  • Generator gen:自定義函數,可以是函數指針或者仿函數對象、lambda表達式
  • 作用:把gen函數的執行結果賦值給[first,last)區間的每個位置
  • 返回值:無返回值
  • 實現:
template <class ForwardIterator, class Generator>void generate ( ForwardIterator first, ForwardIterator last, Generator gen ) {while (first != last) {*first = gen();++first;} }
  • 案例:
// generate algorithm example #include <iostream> // std::cout #include <algorithm> // std::generate #include <vector> // std::vector #include <ctime> // std::time #include <cstdlib> // std::rand, std::srand// function generator: int RandomNumber () { return (std::rand()%100); }// class generator: struct c_unique {int current;c_unique() {current=0;}int operator()() {return ++current;} } UniqueNumber;int main () {std::srand ( unsigned ( std::time(0) ) );std::vector<int> myvector (8);std::generate (myvector.begin(), myvector.end(), RandomNumber);std::cout << "myvector contains:";for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)std::cout << ' ' << *it;std::cout << '\n';std::generate (myvector.begin(), myvector.end(), UniqueNumber);std::cout << "myvector contains:";for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)std::cout << ' ' << *it;std::cout << '\n';return 0; }A possible output: myvector contains: 57 87 76 66 85 54 17 15 myvector contains: 1 2 3 4 5 6 7 8

5.std::generate_n

  • 原型:
//98 template <class OutputIterator, class Size, class Generator>void generate_n (OutputIterator first, Size n, Generator gen);//11 template <class OutputIterator, class Size, class Generator>OutputIterator generate_n (OutputIterator first, Size n, Generator gen);
  • 參數:
  • OutputIterator first:區間的開始
  • Size n:個數
  • Generator gen:自定義函數
  • 作用:把gen函數的返回值作為[first,last)區間前n個元素的值
  • 返回值:11返回最后一個成功賦值位置的下一個位置
  • 實現:
template <class OutputIterator, class Size, class Generator>void generate_n ( OutputIterator first, Size n, Generator gen ) {while (n>0) {*first = gen();++first; --n;} }
  • 案例:
// generate_n example #include <iostream> // std::cout #include <algorithm> // std::generate_nint current = 0; int UniqueNumber () { return ++current; }int main () {int myarray[9];std::generate_n (myarray, 9, UniqueNumber);std::cout << "myarray contains:";for (int i=0; i<9; ++i)std::cout << ' ' << myarray[i];std::cout << '\n';return 0; }A possible output: myarray contains: 1 2 3 4 5 6 7 8 9

6.std::includes

  • 原型:
template <class InputIterator1, class InputIterator2>bool includes ( InputIterator1 first1, InputIterator1 last1,InputIterator2 first2, InputIterator2 last2 );template <class InputIterator1, class InputIterator2, class Compare>bool includes ( InputIterator1 first1, InputIterator1 last1,InputIterator2 first2, InputIterator2 last2, Compare comp );
  • 參數:
  • InputIterator1 first1:區間的開始
  • InputIterator1 last1:區間的結束
  • InputIterator2 first2:區間的開始
  • InputIterator2 last2:區間的結束
  • Compare comp:自定義比較規則
  • 需要排序
  • 作用:[first1,last1)區間是否全部包含[first2,last2)中的所有元素
  • 返回值:是則返回true,反之返回false
  • 實現:
template <class InputIterator1, class InputIterator2>bool includes (InputIterator1 first1, InputIterator1 last1,InputIterator2 first2, InputIterator2 last2) {while (first2!=last2) {if ( (first1==last1) || (*first2 < *first1) ) return false;if (!(*first1 < *first2)) ++first2;++first1;}return true; }
  • 案例:
// includes algorithm example #include <iostream> // std::cout #include <algorithm> // std::includes, std::sortbool myfunction (int i, int j) { return i<j; }int main () {int container[] = {5,10,15,20,25,30,35,40,45,50};int continent[] = {40,30,20,10};std::sort (container,container+10);std::sort (continent,continent+4);// using default comparison:if ( std::includes(container,container+10,continent,continent+4) )std::cout << "container includes continent!\n";// using myfunction as comp:if ( std::includes(container,container+10,continent,continent+4, myfunction) )std::cout << "container includes continent!\n";return 0; }Output: container includes continent! container includes continent!

7.std::inplace_merge

  • 原型:
template <class BidirectionalIterator>void inplace_merge (BidirectionalIterator first, BidirectionalIterator middle,BidirectionalIterator last);template <class BidirectionalIterator, class Compare>void inplace_merge (BidirectionalIterator first, BidirectionalIterator middle,BidirectionalIterator last, Compare comp);
  • 參數:
  • BidirectionalIterator first:區間的開始
  • BidirectionalIterator middle:區間的中間位置,即分割線
  • BidirectionalIterator last:區間的結束
  • Compare comp:自定義比較規則
  • [first,middle)區間和[middle,last)區間都需要有序,功能類似與排序,只不過是兩個有序小區間合并成一個大的有序區間
  • 作用:把[first,middle)區間和[middle,last)區間合并并排序
  • 返回值:無返回值
  • 案例:
// inplace_merge example #include <iostream> // std::cout #include <algorithm> // std::inplace_merge, std::sort, std::copy #include <vector> // std::vectorint main () {int first[] = {5,10,15,20,25};int second[] = {50,40,30,20,10};std::vector<int> v(10);std::vector<int>::iterator it;//5 10 15 20 25std::sort (first,first+5);//10 20 30 40 50std::sort (second,second+5);//5 10 15 20 25 : 10 20 30 40 50it=std::copy (first, first+5, v.begin());std::copy (second,second+5,it);std::inplace_merge (v.begin(),v.begin()+5,v.end());std::cout << "The resulting vector contains:";for (it=v.begin(); it!=v.end(); ++it)std::cout << ' ' << *it;std::cout << '\n';return 0; }Output: The resulting vector contains: 5 10 10 15 20 20 25 30 40 50

8.std::is_heap

  • 原型:
template <class RandomAccessIterator>bool is_heap (RandomAccessIterator first, RandomAccessIterator last);template <class RandomAccessIterator, class Compare>bool is_heap (RandomAccessIterator first, RandomAccessIterator last,Compare comp);
  • 參數:
  • RandomAccessIterator first:區間的開始
  • RandomAccessIterator last:區間的結束
  • Compare comp:自定義比較規則,可以是函數指針或者仿函數對象、lambda表達式
  • 作用:判斷[first,last)區間元素是否構成一個堆
  • 返回值:true代表構成一個堆,反之不構成堆
  • 案例:
// is_heap example #include <iostream> // std::cout #include <algorithm> // std::is_heap, std::make_heap, std::pop_heap #include <vector> // std::vectorint main () {std::vector<int> foo {9,5,2,6,4,1,3,8,7};if (!std::is_heap(foo.begin(),foo.end()))std::make_heap(foo.begin(),foo.end());std::cout << "Popping out elements:";while (!foo.empty()) {std::pop_heap(foo.begin(),foo.end()); // moves largest element to backstd::cout << ' ' << foo.back(); // prints backfoo.pop_back(); // pops element out of container}std::cout << '\n';return 0; }Output: Popping out elements: 9 8 7 6 5 4 3 2 1

9.std::is_heap_until

  • 原型:
template <class RandomAccessIterator>RandomAccessIterator is_heap_until (RandomAccessIterator first,RandomAccessIterator last);template <class RandomAccessIterator, class Compare>RandomAccessIterator is_heap_until (RandomAccessIterator first,RandomAccessIterator lastCompare comp);
  • 參數:
  • RandomAccessIterator first:區間的開始
  • RandomAccessIterator last:區間的結束
  • Compare comp:自定義比較規則,可以是函數指針或者仿函數對象、lambda表達式
  • 作用:求[first,last)區間內第一個導致不滿足堆特性的元素位置
  • 返回值:返回第一個導致不滿足堆特性的元素位置
  • 案例:
#include <iostream> // std::cout #include <algorithm> // std::is_heap_until, std::sort, std::reverse #include <vector> // std::vectorint main () {std::vector<int> foo {2,6,9,3,8,4,5,1,7};std::sort(foo.begin(),foo.end());std::reverse(foo.begin(),foo.end());auto last = std::is_heap_until (foo.begin(),foo.end());std::cout << "The " << (last - foo.begin()) << " first elements are a valid heap:";for (auto it=foo.begin(); it!=last; ++it)std::cout << ' ' << *it;std::cout << '\n';return 0; }Most implementations consider a range sorted in reverse order a valid heap: Possible output: The 9 first elements are a valid heap: 9 8 7 6 5 4 3 2 1

10.std::is_partitioned

  • 原型:
template <class InputIterator, class UnaryPredicate>bool is_partitioned (InputIterator first, InputIterator last, UnaryPredicate pred);
  • 參數:
  • InputIterator first:區間的開始
  • InputIterator last:區間的結束
  • UnaryPredicate pred:自定義比較規則,可以是函數指針或者仿函數對象、lambda表達式
  • 作用:判斷[first,last)區間內的所有滿足pred條件的元素是否都在開始位置,且一旦出現不滿足pred的元素后,后面不能再出現滿足pred條件的元素
  • 返回值:滿足條件返回true,反之返回false
  • 實現:
template <class InputIterator, class UnaryPredicate>bool is_partitioned (InputIterator first, InputIterator last, UnaryPredicate pred) {while (first!=last && pred(*first)) {++first;}while (first!=last) {if (pred(*first)) return false;++first;}return true; }
  • 案例:
// is_partitioned example #include <iostream> // std::cout #include <algorithm> // std::is_partitioned #include <array> // std::arraybool IsOdd (int i) { return (i%2)==1; }int main () {std::array<int,7> foo {1,2,3,4,5,6,7};// print contents:std::cout << "foo:"; for (int& x:foo) std::cout << ' ' << x;if ( std::is_partitioned(foo.begin(),foo.end(),IsOdd) )std::cout << " (partitioned)\n";elsestd::cout << " (not partitioned)\n";// partition array:std::partition (foo.begin(),foo.end(),IsOdd);// print contents again:std::cout << "foo:"; for (int& x:foo) std::cout << ' ' << x;if ( std::is_partitioned(foo.begin(),foo.end(),IsOdd) )std::cout << " (partitioned)\n";elsestd::cout << " (not partitioned)\n";return 0; }Possible output: foo: 1 2 3 4 5 6 7 (not partitioned) foo: 1 7 3 5 4 6 2 (partitioned)

總結

以上是生活随笔為你收集整理的STL中算法锦集(三)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。