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

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

STL中算法锦集(一)

發(fā)布時(shí)間:2024/4/11 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 STL中算法锦集(一) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

STL中算法錦集(一)

文章目錄

  • STL中算法錦集(一)
    • 一、< algorithm >
      • 1.std::adjacent_find
      • 2.std::all_of
      • 3.std::any_of
      • 4.std::binary_search
      • 5.std::copy
      • 6.std::copy_backward
      • 7.std::copy_if
      • 8.std::copy_n
      • 9.std::count
      • 10.std::count_if

一、< algorithm >

雖然這一部分不一定要全部掌握,但是掌握可以更快捷、方便的編程

1.std::adjacent_find

  • 原型:
template <class ForwardIterator>ForwardIterator adjacent_find (ForwardIterator first, ForwardIterator last);template <class ForwardIterator, class BinaryPredicate>ForwardIterator adjacent_find (ForwardIterator first, ForwardIterator last, BinaryPredicate pred);
  • 參數(shù):
  • ForwardIterator first:區(qū)間的開(kāi)始
  • ForwardIterator last:區(qū)間的結(jié)束
  • BinaryPredicate pred:自定義比較規(guī)則,可以是函數(shù)指針或者仿函數(shù)對(duì)象、lambda表達(dá)式
  • 作用:在[first,last)這個(gè)范圍中搜索兩個(gè)連續(xù)、滿(mǎn)足pred條件、且是第一次出現(xiàn)的元素位置
  • 返回值:如果找到這兩個(gè)元素對(duì),返回first迭代器,如果沒(méi)有找到這兩個(gè)元素對(duì),則返回last迭代器
  • 實(shí)現(xiàn):
template <class ForwardIterator> ForwardIterator adjacent_find (ForwardIterator first,ForwardIterator last) {if (first != last){ForwardIterator next = first; ++next;while (next != last) {// or: if (pred(*first,*next))if (*first == *next)return first;++first; ++next;}}return last; }
  • 案例:
// adjacent_find example #include <iostream> // std::cout #include <algorithm> // std::adjacent_find #include <vector> // std::vectorbool myfunction (int i, int j) {return (i==j); }int main () {int myints[] = {5,20,5,30,30,20,10,10,20};std::vector<int> myvector (myints,myints+8);std::vector<int>::iterator it;// using default comparison:it = std::adjacent_find (myvector.begin(), myvector.end());if (it!=myvector.end())std::cout << "the first pair of repeated elements are: " << *it << '\n';//using predicate comparison:it = std::adjacent_find (++it, myvector.end(), myfunction);if (it != myvector.end())std::cout << "the second pair of repeated elements are: " << *it << '\n';return 0; }Output: the first pair of repeated elements are: 30 the second pair of repeated elements are: 10

2.std::all_of

  • 原型:
template <class InputIterator, class UnaryPredicate>bool all_of (InputIterator first, InputIterator last, UnaryPredicate pred);
  • 參數(shù):
  • InputIterator first:區(qū)間的開(kāi)始
  • InputIterator last:區(qū)間的結(jié)束
  • UnaryPredicate pred:自定義比較規(guī)則,可以是函數(shù)指針或者仿函數(shù)對(duì)象、lambda表達(dá)式
  • 作用:判斷[first,last)區(qū)間中的所有元素是否滿(mǎn)足pred條件
  • 返回值:如果全部滿(mǎn)足pred條件返回true,反之有一個(gè)不滿(mǎn)足條件返回false
  • 實(shí)現(xiàn):
template<class InputIterator, class UnaryPredicate>bool all_of (InputIterator first, InputIterator last, UnaryPredicate pred) {while (first != last) {if (!pred(*first)) return false;++first;}return true; }
  • 案例:
// all_of example #include <iostream> // std::cout #include <algorithm> // std::all_of #include <array> // std::arrayint main () {std::array<int,8> foo = {3,5,7,11,13,17,19,23};if ( std::all_of(foo.begin(), foo.end(), [](int i){return i % 2;}) )std::cout << "All the elements are odd numbers.\n";return 0; }Output: All the elements are odd numbers.

3.std::any_of

  • 原型:
template <class InputIterator, class UnaryPredicate>bool any_of (InputIterator first, InputIterator last, UnaryPredicate pred);
  • 參數(shù):
  • InputIterator first:區(qū)間的開(kāi)始
  • InputIterator last:區(qū)間的結(jié)束
  • UnaryPredicate pred:自定義比較規(guī)則,可以是函數(shù)指針或者仿函數(shù)對(duì)象、lambda表達(dá)式
  • 作用:判斷[first,last)區(qū)間中是否有元素是否滿(mǎn)足pred條件
  • 返回值:如果有任意一個(gè)元素滿(mǎn)足pred條件就返回true,反之返回false
  • 實(shí)現(xiàn):
template<class InputIterator, class UnaryPredicate>bool any_of (InputIterator first, InputIterator last, UnaryPredicate pred) {while (first!=last) {if (pred(*first)) return true;++first;}return false; }
  • 案例:
// any_of example #include <iostream> // std::cout #include <algorithm> // std::any_of #include <array> // std::arrayint main () {std::array<int,7> foo = {0,1,-1,3,-3,5,-5};if ( std::any_of(foo.begin(), foo.end(), [](int i){return i < 0;}) )std::cout << "There are negative elements in the range.\n";return 0; }Output: There are negative elements in the range.

4.std::binary_search

  • 原型:
template <class ForwardIterator, class T>bool binary_search (ForwardIterator first, ForwardIterator last,const T& val);template <class ForwardIterator, class T, class Compare>bool binary_search (ForwardIterator first, ForwardIterator last,const T& val, Compare comp);
  • 參數(shù):
  • ForwardIterator first:區(qū)間的開(kāi)始
  • ForwardIterator last:區(qū)間的結(jié)束
  • const T& val:待搜索的val
  • Compare comp:自定義的比較器,因?yàn)樵乜赡苁亲远x的
  • 作用:二分查找區(qū)間中是否包含val元素
  • 返回值:如果包含返回true,反之返回false
  • 實(shí)現(xiàn):
template <class ForwardIterator, class T>bool binary_search (ForwardIterator first, ForwardIterator last, const T& val) {first = std::lower_bound(first,last,val);return (first != last && !(val < *first)); }
  • 案例:
// binary_search example #include <iostream> // std::cout #include <algorithm> // std::binary_search, std::sort #include <vector> // std::vectorbool myfunction (int i,int j) { return (i < j); }int main () {int myints[] = {1,2,3,4,5,4,3,2,1};std::vector<int> v(myints,myints+9); // 1 2 3 4 5 4 3 2 1// using default comparison:std::sort (v.begin(), v.end());std::cout << "looking for a 3... ";if (std::binary_search (v.begin(), v.end(), 3))std::cout << "found!\n"; else std::cout << "not found.\n";// using myfunction as comp:std::sort (v.begin(), v.end(), myfunction);std::cout << "looking for a 6... ";if (std::binary_search (v.begin(), v.end(), 6, myfunction))std::cout << "found!\n"; else std::cout << "not found.\n";return 0; }Output: looking for a 3... found! looking for a 6... not found.

5.std::copy

  • 原型:
template <class InputIterator, class OutputIterator>OutputIterator copy (InputIterator first, InputIterator last, OutputIterator result);
  • 參數(shù):
  • InputIterator first:區(qū)間的開(kāi)始
  • InputIterator last:區(qū)間的結(jié)束
  • OutputIterator result:拷貝區(qū)間的開(kāi)始位置
  • 注意[first,last)區(qū)間和result不能夠重疊,否則會(huì)報(bào)錯(cuò)
  • 作用:把[first,last)區(qū)間當(dāng)中的所有元素拷貝到result空間中
  • 返回值:返回拷貝后的空間末尾位置的迭代器
  • 實(shí)現(xiàn):
template<class InputIterator, class OutputIterator>OutputIterator copy (InputIterator first, InputIterator last, OutputIterator result) {while (first != last) {*result = *first;++result;++first;}return result; }
  • 案例:
// copy algorithm example #include <iostream> // std::cout #include <algorithm> // std::copy #include <vector> // std::vectorint main () {int myints[]={10,20,30,40,50,60,70};std::vector<int> myvector (7);std::copy ( myints, myints+7, myvector.begin() );std::cout << "myvector contains:";for (std::vector<int>::iterator it = myvector.begin(); it != myvector.end(); ++it)std::cout << ' ' << *it;std::cout << '\n';return 0; }Output: myvector contains: 10 20 30 40 50 60 70

6.std::copy_backward

  • 原型:
template <class BidirectionalIterator1, class BidirectionalIterator2>BidirectionalIterator2 copy_backward (BidirectionalIterator1 first,BidirectionalIterator1 last,BidirectionalIterator2 result);
  • 參數(shù):
  • BidirectionalIterator1 first:區(qū)間的開(kāi)始
  • BidirectionalIterator1 last:區(qū)間的結(jié)束
  • BidirectionalIterator2 result:拷貝區(qū)間的末尾位置
  • 注意[first,last)區(qū)間和result能夠不會(huì)重疊,會(huì)報(bào)錯(cuò)
  • copy是正向copy的,而copy_backward是反向copy的,copy_backward區(qū)間如果沖突就會(huì)把前面區(qū)間的元素覆蓋掉
  • 作用:把[first,last)區(qū)間當(dāng)中的所有元素拷貝到result空間中
  • 返回值:返回拷貝后的空間第一個(gè)位置的迭代器
  • 實(shí)現(xiàn):
template<class BidirectionalIterator1, class BidirectionalIterator2>BidirectionalIterator2 copy_backward ( BidirectionalIterator1 first,BidirectionalIterator1 last,BidirectionalIterator2 result ) {while (last != first) *(--result) = *(--last);return result; }
  • 案例:
// copy_backward example #include <iostream> // std::cout #include <algorithm> // std::copy_backward #include <vector> // std::vectorint main () {std::vector<int> myvector;// set some values:for (int i = 1; i <= 5; i++)myvector.push_back(i * 10); // myvector: 10 20 30 40 50myvector.resize(myvector.size() + 3); // allocate space for 3 more elementsstd::copy_backward ( myvector.begin(), myvector.begin()+5, myvector.end() );std::cout << "myvector contains:";for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)std::cout << ' ' << *it;std::cout << '\n';return 0; }Output: myvector contains: 10 20 30 10 20 30 40 50

7.std::copy_if

  • 原型:
template <class InputIterator, class OutputIterator, class UnaryPredicate>OutputIterator copy_if (InputIterator first, InputIterator last,OutputIterator result, UnaryPredicate pred);
  • 參數(shù):
  • InputIterator first:區(qū)間的開(kāi)始
  • InputIterator last:區(qū)間的結(jié)束
  • OutputIterator result:拷貝區(qū)間的開(kāi)始位置
  • UnaryPredicate pred:自定義比較規(guī)則,可以是函數(shù)指針或者仿函數(shù)對(duì)象、lambda表達(dá)式
  • 作用:[first,last)區(qū)間中的元素如果滿(mǎn)足pred條件就copy到result中
  • 返回值:返回拷貝后的空間末尾位置的迭代器
  • 實(shí)現(xiàn):
template <class InputIterator, class OutputIterator, class UnaryPredicate>OutputIterator copy_if (InputIterator first, InputIterator last,OutputIterator result, UnaryPredicate pred) {while (first!=last) {if (pred(*first)) {*result = *first;++result;}++first;}return result; }
  • 案例:
// copy_if example #include <iostream> // std::cout #include <algorithm> // std::copy_if, std::distance #include <vector> // std::vectorint main () {std::vector<int> foo = {25,15,5,-5,-15};std::vector<int> bar (foo.size());// copy only positive numbers:auto it = std::copy_if (foo.begin(), foo.end(), bar.begin(), [](int i){return !(i < 0);} );bar.resize(std::distance(bar.begin(),it)); // shrink container to new sizestd::cout << "bar contains:";for (int& x: bar) std::cout << ' ' << x;std::cout << '\n';return 0; }Output: bar contains: 25 15 5

8.std::copy_n

  • 原型:
template <class InputIterator, class Size, class OutputIterator>OutputIterator copy_n (InputIterator first, Size n, OutputIterator result);
  • 參數(shù):
  • InputIterator first:區(qū)間的開(kāi)始
  • Size n:拷貝元素的個(gè)數(shù)
  • OutputIterator result:拷貝區(qū)間的開(kāi)始位置
  • 作用:從first拷貝n個(gè)元素到result當(dāng)中
  • 返回值:返回拷貝區(qū)間的末尾位置的迭代器
  • 實(shí)現(xiàn):
template<class InputIterator, class Size, class OutputIterator>OutputIterator copy_n (InputIterator first, Size n, OutputIterator result) {while (n > 0) {*result = *first;++result; ++first;--n;}return result; }
  • 案例:
// copy_n algorithm example #include <iostream> // std::cout #include <algorithm> // std::copy #include <vector> // std::vectorint main () {int myints[]={10,20,30,40,50,60,70};std::vector<int> myvector;myvector.resize(7); // allocate space for 7 elementsstd::copy_n ( myints, 7, myvector.begin() );std::cout << "myvector contains:";for (std::vector<int>::iterator it = myvector.begin(); it!=myvector.end(); ++it)std::cout << ' ' << *it;std::cout << '\n';return 0; }Output: myvector contains: 10 20 30 40 50 60 70

9.std::count

  • 原型:
template <class InputIterator, class T>typename iterator_traits<InputIterator>::difference_typecount (InputIterator first, InputIterator last, const T& val);
  • 參數(shù):
  • InputIterator first:區(qū)間的開(kāi)始
  • InputIterator last:區(qū)間的結(jié)束
  • const T& val:待計(jì)數(shù)的val
  • 作用:[first,last)區(qū)間中值為val的元素的個(gè)數(shù)
  • 返回值:返回值為val的元素的個(gè)數(shù)
  • 實(shí)現(xiàn):
template <class InputIterator, class T>typename iterator_traits<InputIterator>::difference_typecount (InputIterator first, InputIterator last, const T& val) {typename iterator_traits<InputIterator>::difference_type ret = 0;while (first!=last) {if (*first == val) ++ret;++first;}return ret; }
  • 案例:
// count algorithm example #include <iostream> // std::cout #include <algorithm> // std::count #include <vector> // std::vectorint main () {// counting elements in array:int myints[] = {10,20,30,30,20,10,10,20}; // 8 elementsint mycount = std::count (myints, myints+8, 10);std::cout << "10 appears " << mycount << " times.\n";// counting elements in container:std::vector<int> myvector (myints, myints+8);mycount = std::count (myvector.begin(), myvector.end(), 20);std::cout << "20 appears " << mycount << " times.\n";return 0; }Output: 10 appears 3 times. 20 appears 3 times.

10.std::count_if

  • 原型:
template <class InputIterator, class UnaryPredicate>typename iterator_traits<InputIterator>::difference_typecount_if (InputIterator first, InputIterator last, UnaryPredicate pred);
  • 參數(shù):
  • InputIterator first:區(qū)間的開(kāi)始
  • InputIterator last:區(qū)間的結(jié)束
  • UnaryPredicate pred:自定義比較規(guī)則,可以是函數(shù)指針或者仿函數(shù)對(duì)象、lambda表達(dá)式
  • 作用:求[first,last)區(qū)間內(nèi)滿(mǎn)足pred條件的元素的個(gè)數(shù)
  • 返回值:滿(mǎn)足pred條件的元素的個(gè)數(shù)
  • 實(shí)現(xiàn):
template <class InputIterator, class UnaryPredicate>typename iterator_traits<InputIterator>::difference_typecount_if (InputIterator first, InputIterator last, UnaryPredicate pred) {typename iterator_traits<InputIterator>::difference_type ret = 0;while (first!=last) {if (pred(*first)) ++ret;++first;}return ret; }
  • 案例:
// count_if example #include <iostream> // std::cout #include <algorithm> // std::count_if #include <vector> // std::vectorbool IsOdd (int i) { return ((i % 2) == 1); }int main () {std::vector<int> myvector;for (int i=1; i<10; i++) myvector.push_back(i); // myvector: 1 2 3 4 5 6 7 8 9int mycount = count_if (myvector.begin(), myvector.end(), IsOdd);std::cout << "myvector contains " << mycount << " odd values.\n";return 0; }Output: myvector contains 5 odd values.

總結(jié)

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

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。