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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

STL中算法锦集(二)

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

STL中算法錦集(二)

文章目錄

  • STL中算法錦集(二)
    • 一、< algorithm >
      • 1.std::equal
      • 2.std::equal_range
      • 3.std::fill
      • 4.std::fill_n
      • 5.std::find
      • 6.std::find_end
      • 7.std::find_first_of

一、< algorithm >

1.std::equal

  • 原型:
template <class InputIterator1, class InputIterator2>bool equal (InputIterator1 first1, InputIterator1 last1,InputIterator2 first2);template <class InputIterator1, class InputIterator2, class BinaryPredicate>bool equal (InputIterator1 first1, InputIterator1 last1,InputIterator2 first2, BinaryPredicate pred);
  • 參數(shù):
  • InputIterator1 first1:第一個區(qū)間的開始
  • InputIterator1 last1:第一個區(qū)間的結(jié)束
  • InputIterator2 first2:第二個區(qū)間的開始
  • BinaryPredicate pred:自定義比較規(guī)則,可以是函數(shù)指針或者仿函數(shù)對象、lambda表達式
  • 注意第一個區(qū)間的長度如果比第二個區(qū)間的長度長,那么肯定返回false;
  • 反之第一個區(qū)間的長度如果比第二個區(qū)間的長度短,程序正常判斷的
  • 作用:[first1,last1)區(qū)間中的每個元素和first2區(qū)間中的對應(yīng)元素是否滿足pred條件
  • 返回值:如果第一個區(qū)間中的每個元素和第二個區(qū)間中的對應(yīng)元素都滿足pred條件,返回true,反之返回false
  • 實現(xiàn):
template <class InputIterator1, class InputIterator2>bool equal ( InputIterator1 first1, InputIterator1 last1, InputIterator2 first2 ) {while (first1!=last1) {// or: if (!pred(*first1,*first2))if (!(*first1 == *first2))return false;++first1; ++first2;}return true; }
  • 案例:
// equal algorithm example #include <iostream> // std::cout #include <algorithm> // std::equal #include <vector> // std::vectorbool mypredicate (int i, int j) {return (i==j); }int main () {int myints[] = {20,40,60,80,100}; // myints: 20 40 60 80 100std::vector<int>myvector (myints,myints+5); // myvector: 20 40 60 80 100// using default comparison:if ( std::equal (myvector.begin(), myvector.end(), myints) )std::cout << "The contents of both sequences are equal.\n";elsestd::cout << "The contents of both sequences differ.\n";myvector[3]=81; // myvector: 20 40 60 81 100// using predicate comparison:if ( std::equal (myvector.begin(), myvector.end(), myints, mypredicate) )std::cout << "The contents of both sequences are equal.\n";elsestd::cout << "The contents of both sequences differ.\n";return 0; }Output: The contents of both sequences are equal. The contents of both sequence differ.

2.std::equal_range

  • 原型:
template <class ForwardIterator, class T>pair<ForwardIterator,ForwardIterator>equal_range (ForwardIterator first, ForwardIterator last, const T& val);template <class ForwardIterator, class T, class Compare>pair<ForwardIterator,ForwardIterator>equal_range (ForwardIterator first, ForwardIterator last, const T& val,Compare comp);
  • 參數(shù):
  • ForwardIterator first:區(qū)間的開始
  • ForwardIterator last:區(qū)間的結(jié)束
  • const T& val:待比較的值
  • Compare comp:自定義比較規(guī)則
  • 需要排序
  • 作用:求[first,last)區(qū)間內(nèi)值為val的范圍,左閉右開區(qū)間
  • 返回值:返回值為一個pair的鍵值對,每個元素都是迭代器,[begin,end):代表值為val的開始位置和結(jié)束位置的下一個位置
  • 實現(xiàn):
template <class ForwardIterator, class T>pair<ForwardIterator,ForwardIterator>equal_range (ForwardIterator first, ForwardIterator last, const T& val) {ForwardIterator it = std::lower_bound (first,last,val);return std::make_pair ( it, std::upper_bound(it,last,val) ); }
  • 案例:
// equal_range example // equal_range example #include <iostream> // std::cout #include <algorithm> // std::equal_range, std::sort #include <vector> // std::vectorbool mygreater (int i,int j) { return (i>j); }int main () {int myints[] = {10,20,30,30,20,10,10,20};std::vector<int> v(myints,myints+8); // 10 20 30 30 20 10 10 20std::pair<std::vector<int>::iterator,std::vector<int>::iterator> bounds;// using default comparison:std::sort (v.begin(), v.end());// 10 10 10 20 20 20 30 30bounds=std::equal_range (v.begin(), v.end(), 20);// using "mygreater" as comp:std::sort (v.begin(), v.end(), mygreater);// 30 30 20 20 20 10 10 10bounds=std::equal_range (v.begin(), v.end(), 20, mygreater); // std::cout << "bounds at positions " << (bounds.first - v.begin());std::cout << " and " << (bounds.second - v.begin()) << '\n';return 0; }Output: bounds at positions 2 and 5

3.std::fill

  • 原型:
template <class ForwardIterator, class T>void fill (ForwardIterator first, ForwardIterator last, const T& val);
  • 參數(shù):
  • ForwardIterator first:區(qū)間的開始
  • ForwardIterator last:區(qū)間的結(jié)束
  • const T& val:填充的val
  • 作用:把[first,last)區(qū)間用val值填充
  • 返回值:沒有返回值
  • 實現(xiàn):
template <class ForwardIterator, class T>void fill (ForwardIterator first, ForwardIterator last, const T& val) {while (first != last) {*first = val;++first;} }
  • 案例:
// fill algorithm example #include <iostream> // std::cout #include <algorithm> // std::fill #include <vector> // std::vectorint main () {std::vector<int> myvector (8);// myvector: 0 0 0 0 0 0 0 0std::fill (myvector.begin(),myvector.begin()+4,5); // myvector: 5 5 5 5 0 0 0 0std::fill (myvector.begin()+3,myvector.end()-2,8); // myvector: 5 5 5 8 8 8 0 0std::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: 5 5 5 8 8 8 0 0

4.std::fill_n

  • 原型:
//98 template <class OutputIterator, class Size, class T>void fill_n (OutputIterator first, Size n, const T& val);//11 template <class OutputIterator, class Size, class T>OutputIterator fill_n (OutputIterator first, Size n, const T& val);
  • 參數(shù):
  • OutputIterator first:區(qū)間的開始
  • Size n
  • const T& val
  • 作用:從first位置開始填充n個val值
  • 返回值:98:沒有返回值。11:返回填充完成后位置的下一個位置
  • 實現(xiàn):
template <class OutputIterator, class Size, class T>OutputIterator fill_n (OutputIterator first, Size n, const T& val) {while (n > 0) {*first = val;++first; --n;}return first; // since C++11 }
  • 案例:
// fill_n example #include <iostream> // std::cout #include <algorithm> // std::fill_n #include <vector> // std::vectorint main () {std::vector<int> myvector (8,10); // myvector: 10 10 10 10 10 10 10 10std::fill_n (myvector.begin(),4,20); // myvector: 20 20 20 20 10 10 10 10std::fill_n (myvector.begin()+3,3,33); // myvector: 20 20 20 33 33 33 10 10std::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: 20 20 20 33 33 33 10 10

5.std::find

  • 原型:
template <class InputIterator, class T>InputIterator find (InputIterator first, InputIterator last, const T& val);
  • 參數(shù):
  • InputIterator first:區(qū)間的開始
  • InputIterator last:區(qū)間的結(jié)束
  • const T& val:待查找的值val
  • 作用:[first,last)區(qū)間內(nèi)查找第一個值為val的元素的位置
  • 返回值:返回第一個值為val的元素的位置
  • 實現(xiàn):
template<class InputIterator, class T>InputIterator find (InputIterator first, InputIterator last, const T& val) {while (first!=last) {if (*first==val) return first;++first;}return last; }
  • 案例:
// find example #include <iostream> // std::cout #include <algorithm> // std::find #include <vector> // std::vectorint main () {// using std::find with array and pointer:int myints[] = { 10, 20, 30, 40 };int * p;p = std::find (myints, myints+4, 30);if (p != myints+4)std::cout << "Element found in myints: " << *p << '\n';elsestd::cout << "Element not found in myints\n";// using std::find with vector and iterator:std::vector<int> myvector (myints,myints+4);std::vector<int>::iterator it;it = find (myvector.begin(), myvector.end(), 30);if (it != myvector.end())std::cout << "Element found in myvector: " << *it << '\n';elsestd::cout << "Element not found in myvector\n";return 0; }Output: Element found in myints: 30 Element found in myvector: 30

6.std::find_end

  • 原型:
template <class ForwardIterator1, class ForwardIterator2>ForwardIterator1 find_end (ForwardIterator1 first1, ForwardIterator1 last1,ForwardIterator2 first2, ForwardIterator2 last2);template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>ForwardIterator1 find_end (ForwardIterator1 first1, ForwardIterator1 last1,ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
  • 參數(shù):
  • ForwardIterator1 first1:區(qū)間1的開始
  • ForwardIterator1 last1:區(qū)間1的結(jié)束
  • ForwardIterator2 first2:區(qū)間2的開始
  • ForwardIterator2 last2:區(qū)間2的結(jié)束
  • BinaryPredicate pred:自定義比較規(guī)則,可以是函數(shù)指針或者仿函數(shù)對象、lambda表達式
  • 作用:在[first1,last1)區(qū)間的查找滿足[first2,last2)區(qū)間中的所有元素
  • 返回值:如果[first1,last1)區(qū)間查找到[first2,last2)區(qū)間中的所有元素,返回[first2,last2)區(qū)間在[first1,last1)區(qū)間最后一個滿足條件的開始位置
  • 實現(xiàn):
template<class ForwardIterator1, class ForwardIterator2>ForwardIterator1 find_end (ForwardIterator1 first1, ForwardIterator1 last1,ForwardIterator2 first2, ForwardIterator2 last2) {if (first2==last2) return last1; // specified in C++11ForwardIterator1 ret = last1;while (first1 != last1){ForwardIterator1 it1 = first1;ForwardIterator2 it2 = first2;// or: while (pred(*it1,*it2))while (*it1 == *it2) {++it1; ++it2;if (it2==last2) { ret=first1; break; }if (it1==last1) return ret;}++first1;}return ret; }
  • 案例:
// find_end example #include <iostream> // std::cout #include <algorithm> // std::find_end #include <vector> // std::vectorbool myfunction (int i, int j) {return (i==j); }int main () {int myints[] = {1,2,3,4,5,1,2,3,4,5};std::vector<int> haystack (myints,myints+10);int needle1[] = {1,2,3};// using default comparison:std::vector<int>::iterator it;it = std::find_end (haystack.begin(), haystack.end(), needle1, needle1+3);if (it!=haystack.end())std::cout << "needle1 last found at position " << (it-haystack.begin()) << '\n';int needle2[] = {4,5,1};// using predicate comparison:it = std::find_end (haystack.begin(), haystack.end(), needle2, needle2+3, myfunction);if (it!=haystack.end())std::cout << "needle2 last found at position " << (it-haystack.begin()) << '\n';return 0; }Output: needle1 found at position 5 needle2 found at position 3

7.std::find_first_of

  • 原型:
//98: template <class ForwardIterator1, class ForwardIterator2>ForwardIterator1 find_first_of (ForwardIterator1 first1, ForwardIterator1 last1,ForwardIterator2 first2, ForwardIterator2 last2);template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>ForwardIterator1 find_first_of (ForwardIterator1 first1, ForwardIterator1 last1,ForwardIterator2 first2, ForwardIterator2 last2,BinaryPredicate pred);//11: template <class InputIterator, class ForwardIterator>InputIterator find_first_of (InputIterator first1, InputIterator last1,ForwardIterator first2, ForwardIterator last2);template <class InputIterator, class ForwardIterator, class BinaryPredicate>InputIterator find_first_of (InputIterator first1, InputIterator last1,ForwardIterator first2, ForwardIterator last2,BinaryPredicate pred);
  • 參數(shù):
  • InputIterator first1:區(qū)間的開始
  • InputIterator last1:區(qū)間的結(jié)束
  • ForwardIterator first2:區(qū)間的開始
  • ForwardIterator last2:區(qū)間的結(jié)束
  • BinaryPredicate pred:自定義比較規(guī)則,可以是函數(shù)指針或者仿函數(shù)對象、lambda表達式
  • 作用:在[first1,last1)區(qū)間內(nèi)找[first2,last2)區(qū)間內(nèi)任意一個元素出現(xiàn)的位置
  • 返回值:如果在[first1,last1)區(qū)間內(nèi)找[first2,last2)區(qū)間內(nèi)任意一個元素返回該位置的迭代器
  • 實現(xiàn):
template<class InputIterator, class ForwardIterator>InputIterator find_first_of ( InputIterator first1, InputIterator last1,ForwardIterator first2, ForwardIterator last2) {while (first1!=last1) {for (ForwardIterator it=first2; it!=last2; ++it) {// or: if (pred(*it,*first))if (*it==*first1) return first1;}++first1;}return last1; }
  • 案例:
// find_first_of example #include <iostream> // std::cout #include <algorithm> // std::find_first_of #include <vector> // std::vector #include <cctype> // std::tolowerbool comp_case_insensitive (char c1, char c2) {return (std::tolower(c1)==std::tolower(c2)); }int main () {int mychars[] = {'a','b','c','A','B','C'};std::vector<char> haystack (mychars,mychars+6);std::vector<char>::iterator it;int needle[] = {'A','B','C'};// using default comparison:it = find_first_of (haystack.begin(), haystack.end(), needle, needle+3);if (it!=haystack.end())std::cout << "The first match is: " << *it << '\n';// using predicate comparison:it = find_first_of (haystack.begin(), haystack.end(),needle, needle+3, comp_case_insensitive);if (it!=haystack.end())std::cout << "The first match is: " << *it << '\n';return 0; }Output: The first match is: A The first match is: a 超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術(shù)人生

總結(jié)

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

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