欢迎访问 生活随笔!

生活随笔

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

编程问答

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

发布时间:2023/12/13 编程问答 34 豆豆
生活随笔 收集整理的这篇文章主要介绍了 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的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。