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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

STL源码剖析 基本算法 equal | fill | iter_awap | lexicographical_compare | max | min | swap |mismatch

發布時間:2023/12/13 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 STL源码剖析 基本算法 equal | fill | iter_awap | lexicographical_compare | max | min | swap |mismatch 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Equal

  • 兩個序列在[first,last)區間內相等,equal()返回true。以第一個序列作為基準,如果第二序列元素多不予考慮,如果要保證兩個序列完全相等需要比較元素的個數
if(vec1.size() == vec2.size() && equal(vec1.begin(),vec1.end(),vec2.begin()));
  • 或者可以使用容器提供的equality操作符,例如 vec1 == vec2
  • 如果第二序列比第一序列的元素少 ,算法內部進行迭代行為的時候,會超越序列的尾端,造成不可預測的結果
  • 第一版本缺省使用元素型別所提供的equality 操作符來進行大小的比較;第二版本使用指定的仿函數pred作為比較的依據?
template <class InputIterator1,class InputIterator2> inline bool equal(InputIterator1 first1,InputIterator1 last1,InputIterator2 first2){//將序列一走過一遍,序列二亦步亦趨//如果序列一的元素多過序列二的元素個數 就會糟糕了while(first1 != first2){if(!(*first1 == *first2)){return false;}++first1;++first2;}//第二個版本 // for (; first1 != last1;++first1,++first2){ // if(*first1 != *first2){ // return false; // } // }return true; }//使用自定義的 仿函數pred作為比較的依據 template <class InputIterator1,class InputIterator2,class BinaryOperation> inline bool equal(InputIterator1 first1,InputIterator1 last1,InputIterator2 first2,BinaryOperation binary_pred){while(first1 != first2){if(!binary_pred(*first1,*first2)){return false;}++first1;++first2;}return true; } #include <iostream> //std::cout #include <algorithm> //std::equal #include <vector> //std::vectorbool my_predicate(int i,int j){return (i==j); }int main(){int myints[] = {20,40,60,80,100};std::vector<int>my_vector(myints,myints+5);//using default comparison:if(std::equal(my_vector.begin(),my_vector.end(),myints)){std::cout << " equal! "<< std::endl;}else{std::cout << " differ! " << std::endl;}my_vector[3] = 94;if(std::equal(my_vector.begin(),my_vector.end(),myints, my_predicate)){std::cout << " equal! "<< std::endl;}else{std::cout << " differ! " << std::endl;}return 0; }

fill

  • 將指定[first,last)內的所有元素使用新值填充
template<class ForwardIterator,class T> void fill(ForwardIterator first,ForwardIterator last,const T& value){while (first!=last){*first = value; //設定新值++first; //迭代走過整個區間} } #include <iostream> //std::cout #include <algorithm> //std::fill #include <vector> //std::vectorbool my_predicate(int i,int j){return (i==j); }int main(){std::vector<int>my_vector(8); //0 0 0 0 0 0 0 0std::fill(my_vector.begin(),my_vector.begin()+4,5);std::fill(my_vector.begin()+3,my_vector.end()-2,8);std::cout << "my_vector contains:"<< std::endl;for(std::vector<int>::iterator it = my_vector.begin();it != my_vector.end();++it){std::cout << *it << " ";}return 0; }

?fill_n

  • 將本地[first,last)內的前n個元素改寫為新的數值,返回迭代器指向被填入的最后一個元素的下一個位置
template<class OutputIterator,class Size,class T> OutputIterator fill_n(OutputIterator first,Size n,const T&value){while (n>0){*first = value;--n;++first;}return first; }
  • 如果n超過了容器的容量的上限,會造成什么樣的后果呢??
int ia[3] = {0,1,2};std::vector<int>iv(ia,ia+3);std::fill_n(iv.begin(),5,7);
  • 迭代器進行的是assignment操作是一種覆寫操作,一旦超越了容器的大小會造成不可預期的結果。
  • 解決方法:使用insert產生一個具有插入性質而不是覆寫能力的迭代器。inserter()可以產生一個用來修飾迭代器的配接器,用法如下?
int ia[3] = {0,1,2};std::vector<int>iv(ia,ia+3); // std::fill_n(iv.begin(),5,7); //7 7 7std::fill_n(std::inserter(iv,iv.begin()),5,7); // 7 7 7 7 7 0 1 2

iter_swap

  • ? ?

template <class ForwardIterator1, class ForwardIterator2>void iter_swap (ForwardIterator1 a, ForwardIterator2 b) {swap (*a, *b); } // iter_swap example #include <iostream> // std::cout #include <algorithm> // std::iter_swap #include <vector> // std::vectorint main () {int myints[]={10,20,30,40,50 }; // myints: 10 20 30 40 50std::vector<int> myvector (4,99); // myvector: 99 99 99 99std::iter_swap(myints,myvector.begin()); // myints: [99] 20 30 40 50// myvector: [10] 99 99 99std::iter_swap(myints+3,myvector.begin()+2); // myints: 99 20 30 [99] 50// myvector: 10 99 [40] 99std::cout << "myvector contains:";for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)std::cout << ' ' << *it;std::cout << '\n';return 0; }

?lexicographical_compare

  • 操作指針比較[first1 , last1) 和?[first2?, last2)對應位置上的元素大小的比較,持續到(1)某一組元素數據不相等;(2)二者完全相等,同時到達兩個隊列的末尾(3)到達last1或者last2
  • 要求第一序列按照字典排序方式而言不小于第二序列

  • lexicographical_compare - C++ Reference?
template<class InputIterator1,class InputIterator2> bool lexicographical_compare(InputIterator1 first1,InputIterator1 last1,InputIterator2 first2,InputIterator2 last2){while (first1!=last1){if (first2 == last2 || *first2 < *first1){return false;} else if(*first1 < *first2){return true;}++first1;++first2;}return (first2 != last2); } // lexicographical_compare example #include <iostream> // std::cout, std::boolalpha #include <algorithm> // std::lexicographical_compare #include <cctype> // std::tolower// a case-insensitive comparison function: bool mycomp (char c1, char c2) { return std::tolower(c1)<std::tolower(c2); }int main () {char foo[]="Apple";char bar[]="apartment";std::cout << std::boolalpha;std::cout << "Comparing foo and bar lexicographically (foo<bar):\n";std::cout << "Using default comparison (operator<): ";std::cout << std::lexicographical_compare(foo,foo+5,bar,bar+9);std::cout << '\n';std::cout << "Using mycomp as comparison object: ";std::cout << std::lexicographical_compare(foo,foo+5,bar,bar+9,mycomp);std::cout << '\n';return 0; }

?max

  • 取兩個對象中的最大數值
  • 版本一使用greater-than操作符號進行大小的比較,版本二使用自定義comp函數比較
template <class T> const T&max(const T&a,const T&b){return (a>b)?a:b; }template <class T,class BinaryOperation> const T&max(const T&a,const T&b,BinaryOperation binary_pred){return (binary_pred(a,b))?a:b; }

min

  • 取兩個對象中的最小數值
  • 版本一使用less-than操作符號進行大小的比較,版本二使用自定義comp函數比較
template <class T> const T&min(const T&a,const T&b){return (a<b)?a:b; }template <class T,class BinaryOperation> const T&min(const T&a,const T&b,BinaryOperation binary_pred){return (binary_pred(a,b))?a:b; }

mismatch

  • 判斷兩個區間的第一個不匹配的點,返回一個由兩個迭代器組成的pair;pair的第一個迭代器指向的是第一區間第一個不匹配的點,第二個迭代器指向第二個區間的不匹配的點
  • 需要首先檢查迭代器是否不等于容器的end()?
  • 如果兩個序列的所有元素對應都匹配,返回的是兩個序列各自的last迭代器,缺省情況下使用equality操作符號來比較元素;第二版本允許用戶指定自己的比較操作
  • 需要第二個序列的元素個數要大于第一個序列,否則會發生不可預期的行為

template <class InputIterator1,class InputIterator2> std::pair<InputIterator1,InputIterator2>mismatch(InputIterator1 first1,InputIterator1 last1,InputIterator2 first2){while ((first1!=last1)&&(*first1 == *first2)){++first1;++first2;}return std::make_pair(first1,first2); }
  • 第二版本 pred(*first1,*first2)?

swap

  • 對調元素的內容
template <class T> inline void swap(T&a,T&b){T tmp (a);a = b;b = tmp; }

總結

以上是生活随笔為你收集整理的STL源码剖析 基本算法 equal | fill | iter_awap | lexicographical_compare | max | min | swap |mismatch的全部內容,希望文章能夠幫你解決所遇到的問題。

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