加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

STL源码剖析 - 第6章 算法 - 6.7.1 数据处理算法 - 三

发布时间:2020-12-14 02:42:40 所属栏目:大数据 来源:网络整理
导读:1、?replace //将区间[first,last)内的所有old_value都以new_value替代. template class _ForwardIter,class _Tp void replace(_ForwardIter __first,_ForwardIter __last,const _Tp __old_value,const _Tp __new_value) { __STL_REQUIRES(_ForwardIter,_Mut

1、?replace

//将区间[first,last)内的所有old_value都以new_value替代.  
template <class _ForwardIter,class _Tp>  
void replace(_ForwardIter __first,_ForwardIter __last,const _Tp& __old_value,const _Tp& __new_value) {  
  __STL_REQUIRES(_ForwardIter,_Mutable_ForwardIterator);  
  __STL_REQUIRES_BINARY_OP(_OP_EQUAL,bool,typename iterator_traits<_ForwardIter>::value_type,_Tp);  
  __STL_CONVERTIBLE(_Tp,typename iterator_traits<_ForwardIter>::value_type);  
  for ( ; __first != __last; ++__first)  
      //将区间内所有old_value都以new_value替代.  
    if (*__first == __old_value)  
      *__first = __new_value;  
}  


2、?replace_if

//将区间[first,last)内的所有被pred判断为true的元素都以new_value替代.  
template <class _ForwardIter,class _Predicate,class _Tp>  
void replace_if(_ForwardIter __first,_Predicate __pred,_Mutable_ForwardIterator);  
  __STL_CONVERTIBLE(_Tp,typename iterator_traits<_ForwardIter>::value_type);  
  __STL_UNARY_FUNCTION_CHECK(_Predicate,typename iterator_traits<_ForwardIter>::value_type);  
  for ( ; __first != __last; ++__first)  
    if (__pred(*__first))//pred判断为true  
      *__first = __new_value;//修改其值  
}  


3、?replace_copy

//将区间[first,last)内的所有old_value都以new_value替代.将新序列复制到result所指的容器中  
//原始容器的内容并不会改变  
template <class _InputIter,class _OutputIter,class _Tp>  
_OutputIter replace_copy(_InputIter __first,_InputIter __last,_OutputIter __result,const _Tp& __new_value) {  
  __STL_REQUIRES(_InputIter,_InputIterator);  
  __STL_REQUIRES(_OutputIter,_OutputIterator);  
  __STL_REQUIRES_BINARY_OP(_OP_EQUAL,typename iterator_traits<_InputIter>::value_type,_Tp);  
  for ( ; __first != __last; ++__first,++__result)  
    *__result = *__first == __old_value ? __new_value : *__first;  
  return __result;  
}  


4、replace_copy_if??

//将区间[first,last)内的所有被pred判断为true的元素都以new_value替代.将新序列复制到result所指的容器中  
//原始容器的内容并不会改变  
template <class _InputIter,class _Tp>  
_OutputIter replace_copy_if(_InputIter __first,_OutputIterator);  
  __STL_UNARY_FUNCTION_CHECK(_Predicate,typename iterator_traits<_InputIter>::value_type);  
  for ( ; __first != __last; ++__first,++__result)  
    *__result = __pred(*__first) ? __new_value : *__first;  
  return __result;  
}  


5、?reverse??

//将序列[first,last)的所有元素在原容器中颠倒重排  
//若迭代器类型为bidirectional_iterator_tag,则调用此函数  
template <class _BidirectionalIter>  
void __reverse(_BidirectionalIter __first,_BidirectionalIter __last,bidirectional_iterator_tag) {  
  while (true)  
    if (__first == __last || __first == --__last)//这里需注意,每次判断last迭代器都会后退一位  
      return;  
    else  
      iter_swap(__first++,__last);//单向交换迭代器所指的元素  
}  
//若迭代器类型为random_access_iterator_tag,则调用此函数  
template <class _RandomAccessIter>  
void __reverse(_RandomAccessIter __first,_RandomAccessIter __last,random_access_iterator_tag) {  
  while (__first < __last)//遍历容器  
    iter_swap(__first++,--__last);//交换两端迭代器所指的元素  
}  
//将序列[first,last)的所有元素在原容器中颠倒重排  
template <class _BidirectionalIter>  
inline void reverse(_BidirectionalIter __first,_BidirectionalIter __last) {  
  __STL_REQUIRES(_BidirectionalIter,_Mutable_BidirectionalIterator);  
  //首先萃取出迭代器的类型  
  __reverse(__first,__last,__ITERATOR_CATEGORY(__first));  
}  


6、reverse_copy

//行为类似reverse,但产生的新序列会被置于以result指出的容器中  
template <class _BidirectionalIter,class _OutputIter>  
_OutputIter reverse_copy(_BidirectionalIter __first,_OutputIter __result) {  
  __STL_REQUIRES(_BidirectionalIter,_BidirectionalIterator);  
  __STL_REQUIRES(_OutputIter,_OutputIterator);  
  while (__first != __last) {//遍历容器  
    --__last;//尾端前移一个位置  
    *__result = *__last;//result容器的起始位置元素值为原始容器尾端元素值  
    ++__result;//更新result,使其前进一个位置  
  }  
  return __result;  
}  


??

7、?rotate??

//将区间[first,middle)内的元素和[middle,last)内的元素互换。minddle所指的元素会成为容器的第一个元素  
//例如对序列{1,2,3,4,5,6,7},对元素3进行旋转操作,则结果为{3,7,1,2}  
 
//迭代器类型为forward_iterator_tag,调用此函数  
template <class _ForwardIter,class _Distance>  
_ForwardIter __rotate(_ForwardIter __first,_ForwardIter __middle,_Distance*,forward_iterator_tag) {  
  if (__first == __middle)  
    return __last;  
  if (__last  == __middle)  
    return __first;  
  
  _ForwardIter __first2 = __middle;  
  do {  
    swap(*__first++,*__first2++);  
    if (__first == __middle)  
      __middle = __first2;  
  } while (__first2 != __last);  
  
  _ForwardIter __new_middle = __first;  
  
  __first2 = __middle;  
  
  while (__first2 != __last) {  
    swap (*__first++,*__first2++);  
    if (__first == __middle)  
      __middle = __first2;  
    else if (__first2 == __last)  
      __first2 = __middle;  
  }  
  
  return __new_middle;  
}  
  
//迭代器类型为bidirectional_iterator_tag,调用此函数  
template <class _BidirectionalIter,class _Distance>  
_BidirectionalIter __rotate(_BidirectionalIter __first,_BidirectionalIter __middle,bidirectional_iterator_tag) {  
  __STL_REQUIRES(_BidirectionalIter,_Mutable_BidirectionalIterator);  
  if (__first == __middle)  
    return __last;  
  if (__last  == __middle)  
    return __first;  
  
  __reverse(__first,__middle,bidirectional_iterator_tag());  
  __reverse(__middle,bidirectional_iterator_tag());  
  
  while (__first != __middle && __middle != __last)  
    swap (*__first++,*--__last);  
  
  if (__first == __middle) {  
    __reverse(__middle,bidirectional_iterator_tag());  
    return __last;  
  }  
  else {  
    __reverse(__first,bidirectional_iterator_tag());  
    return __first;  
  }  
}  
//迭代器类型为Random_iterator_tag,调用此函数  
template <class _RandomAccessIter,class _Distance,class _Tp>  
_RandomAccessIter __rotate(_RandomAccessIter __first,_RandomAccessIter __middle,_Distance *,_Tp *) {  
  __STL_REQUIRES(_RandomAccessIter,_Mutable_RandomAccessIterator);  
  _Distance __n = __last   - __first;  
  _Distance __k = __middle - __first;  
  _Distance __l = __n - __k;  
  _RandomAccessIter __result = __first + (__last - __middle);  
  
  if (__k == 0)  
    return __last;  
  
  else if (__k == __l) {  
    swap_ranges(__first,__middle);  
    return __result;  
  }  
  
  _Distance __d = __gcd(__n,__k);  
  
  for (_Distance __i = 0; __i < __d; __i++) {  
    _Tp __tmp = *__first;  
    _RandomAccessIter __p = __first;  
  
    if (__k < __l) {  
      for (_Distance __j = 0; __j < __l/__d; __j++) {  
        if (__p > __first + __l) {  
          *__p = *(__p - __l);  
          __p -= __l;  
        }  
  
        *__p = *(__p + __k);  
        __p += __k;  
      }  
    }  
  
    else {  
      for (_Distance __j = 0; __j < __k/__d - 1; __j ++) {  
        if (__p < __last - __k) {  
          *__p = *(__p + __k);  
          __p += __k;  
        }  
  
        *__p = * (__p - __l);  
        __p -= __l;  
      }  
    }  
  
    *__p = __tmp;  
    ++__first;  
  }  
  
  return __result;  
}  
//将区间[first,2}  
template <class _ForwardIter>  
inline _ForwardIter rotate(_ForwardIter __first,_ForwardIter __last) {  
  __STL_REQUIRES(_ForwardIter,_Mutable_ForwardIterator);  
  //萃取出迭代器的类型,根据迭代器的类型调用不同的函数  
  return __rotate(__first,__DISTANCE_TYPE(__first),__ITERATOR_CATEGORY(__first));  
}


8、rotate_copy

//将区间[first,last)内的元素互换。middle所指的元素会成为新容器result的第一个元素  
template <class _ForwardIter,class _OutputIter>  
_OutputIter rotate_copy(_ForwardIter __first,_OutputIter __result) {  
  __STL_REQUIRES(_ForwardIter,_ForwardIterator);  
  __STL_REQUIRES(_OutputIter,_OutputIterator);  
  //这里直接采用复制操作,先把[middle,last)复制到result容器中,  
  //再把[first,middle)内容复制到result容器中  
  return copy(__first,copy(__middle,__result));  
}  

?

9、?search.??

//在序列一[first1,last1)所涵盖的区间中,查找序列二[first2,last2)的首次出现点  
//该查找函数有两个版本:  
//版本一:使用默认的equality操作operator==  
//版本二:用户根据需要自行指定操作规则  
/*search函数功能:Searches the range [first1,last1) for the first occurrence of the sequence defined by [first2,last2),and returns an iterator to its first element,or last1 if no occurrences are found. 
 
search函数的原型: 
equality (1):版本一 
    template <class ForwardIterator1,class ForwardIterator2> 
    ForwardIterator1 search (ForwardIterator1 first1,ForwardIterator1 last1,ForwardIterator2 first2,ForwardIterator2 last2); 
predicate (2):版本二    
    template <class ForwardIterator1,class ForwardIterator2,class BinaryPredicate> 
    ForwardIterator1 search (ForwardIterator1 first1,ForwardIterator2 last2,BinaryPredicate pred); 
*/  
//版本一:使用默认的equality操作operator==  
template <class _ForwardIter1,class _ForwardIter2>  
_ForwardIter1 search(_ForwardIter1 __first1,_ForwardIter1 __last1,_ForwardIter2 __first2,_ForwardIter2 __last2)   
{  
  __STL_REQUIRES(_ForwardIter1,_ForwardIterator);  
  __STL_REQUIRES(_ForwardIter2,_ForwardIterator);  
  __STL_REQUIRES_BINARY_OP(_OP_EQUAL,typename iterator_traits<_ForwardIter1>::value_type,typename iterator_traits<_ForwardIter2>::value_type);  
  
  // Test for empty ranges  
  if (__first1 == __last1 || __first2 == __last2)  
    return __first1;  
  
  // Test for a pattern of length 1.  
  _ForwardIter2 __tmp(__first2);  
  ++__tmp;  
  if (__tmp == __last2)  
    return find(__first1,__last1,*__first2);  
  
  // General case.  
  
  _ForwardIter2 __p1,__p;  
  
  __p1 = __first2; ++__p1;  
  
  _ForwardIter1 __current = __first1;  
  
  while (__first1 != __last1) {//若还没到达区间尾端  
    __first1 = find(__first1,*__first2);//查找*first2在区间[first1,last1)首次出现的位置  
    if (__first1 == __last1)//若在[first1,last1)中不存在*first2,即在[first1,last1)不存在子序列[first2,last2)  
      return __last1;//则直接返回区间尾端  
  
    __p = __p1;  
    __current = __first1;   
    if (++__current == __last1)//若[first1,last1)只有一个元素,即序列[first1,last1)小于序列[first2,last2)  
      return __last1;//不可能成为其子序列,返回last1  
  
    while (*__current == *__p) {//若两个序列相对应的值相同  
      if (++__p == __last2)//若序列[first2,last2)只有两个元素,且与序列一匹配  
        return __first1;//则返回匹配的首次位置  
      if (++__current == __last1)//若第一个序列小于第二个序列  
        return __last1;//返回last1  
    }  
  
    ++__first1;  
  }  
  return __first1;  
}  
  
//版本二:用户根据需要自行指定操作规则  
template <class _ForwardIter1,class _ForwardIter2,class _BinaryPred>  
_ForwardIter1 search(_ForwardIter1 __first1,_ForwardIter2 __last2,_BinaryPred  __predicate)   
{  
  __STL_REQUIRES(_ForwardIter1,_ForwardIterator);  
  __STL_BINARY_FUNCTION_CHECK(_BinaryPred,typename iterator_traits<_ForwardIter2>::value_type);  
  
  // Test for empty ranges  
  if (__first1 == __last1 || __first2 == __last2)  
    return __first1;  
  
  // Test for a pattern of length 1.  
  _ForwardIter2 __tmp(__first2);  
  ++__tmp;  
  if (__tmp == __last2) {  
    while (__first1 != __last1 && !__predicate(*__first1,*__first2))  
      ++__first1;  
    return __first1;      
  }  
  
  // General case.  
  
  _ForwardIter2 __p1,__p;  
  
  __p1 = __first2; ++__p1;  
  
  _ForwardIter1 __current = __first1;  
  
  while (__first1 != __last1) {  
    while (__first1 != __last1) {  
      if (__predicate(*__first1,*__first2))  
        break;  
      ++__first1;  
    }  
    while (__first1 != __last1 && !__predicate(*__first1,*__first2))  
      ++__first1;  
    if (__first1 == __last1)  
      return __last1;  
  
    __p = __p1;  
    __current = __first1;   
    if (++__current == __last1) return __last1;  
  
    while (__predicate(*__current,*__p)) {  
      if (++__p == __last2)  
        return __first1;  
      if (++__current == __last1)  
        return __last1;  
    }  
  
    ++__first1;  
  }  
  return __first1;  
}  


10、search_n

//在序列[first,last)查找连续count个符合条件值value元素的位置  
//该查找函数有两个版本:  
//版本一:使用默认的equality操作operator==  
//版本二:用户根据需要自行指定操作规则  
/*search_n函数功能:Searches the range [first,last) for a sequence of count elements,each comparing equal to val (or for which pred returns true). 
 
 
search_n函数的原型: 
equality (1):版本一     
    template <class ForwardIterator,class Size,class T> 
    ForwardIterator search_n (ForwardIterator first,ForwardIterator last,Size count,const T& val); 
predicate (2):版本二    
    template <class ForwardIterator,class T,class BinaryPredicate> 
    ForwardIterator search_n ( ForwardIterator first,const T& val,BinaryPredicate pred ); 
*/  
//版本一:使用默认的equality操作operator==  
template <class _ForwardIter,class _Integer,class _Tp>  
_ForwardIter search_n(_ForwardIter __first,_Integer __count,const _Tp& __val) {  
  __STL_REQUIRES(_ForwardIter,_ForwardIterator);  
  __STL_REQUIRES(typename iterator_traits<_ForwardIter>::value_type,_EqualityComparable);  
  __STL_REQUIRES(_Tp,_EqualityComparable);  
  
  if (__count <= 0)  
    return __first;  
  else {//首先查找value第一次出现的位置  
    __first = find(__first,__val);  
    while (__first != __last) {//若出现的位置不是区间尾端  
      _Integer __n = __count - 1;//更新个数,下面只需查找n=count-1个连续相同value即可  
      _ForwardIter __i = __first;  
      ++__i;//从当前位置的下一个位置开始查找  
      //若没有到达区间尾端,且个数n大于0,且区间元素与value值相等  
      while (__i != __last && __n != 0 && *__i == __val) {  
        ++__i;//继续查找  
        --__n;//减少查找的次数,因为已经找到value再次出现  
      }  
      if (__n == 0)//若区间尚未到达尾端,但是count个value已经查找到  
        return __first;//则输出查找到的首次出现value的位置  
      else  
        __first = find(__i,__val);//若尚未找到连续count个value值的位置,则找出value下次出现的位置,并准备下一次while循环  
    }  
    return __last;  
  }  
}  
//版本二:用户根据需要自行指定操作规则  
template <class _ForwardIter,class _Tp,class _BinaryPred>  
_ForwardIter search_n(_ForwardIter __first,const _Tp& __val,_BinaryPred __binary_pred) {  
  __STL_REQUIRES(_ForwardIter,_Tp);  
  if (__count <= 0)  
    return __first;  
  else {  
    while (__first != __last) {  
      if (__binary_pred(*__first,__val))  
        break;  
      ++__first;  
    }  
    while (__first != __last) {  
      _Integer __n = __count - 1;  
      _ForwardIter __i = __first;  
      ++__i;  
      while (__i != __last && __n != 0 && __binary_pred(*__i,__val)) {  
        ++__i;  
        --__n;  
      }  
      if (__n == 0)  
        return __first;  
      else {  
        while (__i != __last) {  
          if (__binary_pred(*__i,__val))  
            break;  
          ++__i;  
        }  
        __first = __i;  
      }  
    }  
    return __last;  
  }  
}   
//search和search_n函数举例:  
/* 
    #include <iostream>     // std::cout 
    #include <algorithm>    // std::search_n 
    #include <vector>       // std::vector 
 
    bool mypredicate (int i,int j) { 
      return (i==j); 
    } 
 
    int main () { 
      int myints[]={10,20,30,10,20}; 
      std::vector<int> myvector (myints,myints+8); 
      std::vector<int>::iterator it; 
 
      // using default comparison: 
      it = std::search_n (myvector.begin(),myvector.end(),30); 
      if (it!=myvector.end()) 
        std::cout << "two 30s found at position " << (it-myvector.begin()) << 'n'; 
      else 
        std::cout << "match not foundn"; 
 
      // using predicate comparison: 
      it = std::search_n (myvector.begin(),mypredicate); 
      if (it!=myvector.end()) 
        std::cout << "two 10s found at position " << int(it-myvector.begin()) << 'n'; 
      else 
        std::cout << "match not foundn"; 
     
      int needle1[] = {10,20}; 
   
      // using default comparison: 
      it = std::search (myvector.begin(),needle1,needle1+2);   
       if (it!=myvector.end()) 
        std::cout << "needle1 found at position " << (it-myvector.begin()) << 'n'; 
      else 
        std::cout << "needle1 not foundn"; 
     
      // using predicate comparison: 
      int needle2[] = {30,10}; 
      it = std::search (myvector.begin(),needle2,needle2+3,mypredicate); 
      if (it!=myvector.end()) 
        std::cout << "needle2 found at position " << (it-myvector.begin()) << 'n'; 
      else 
        std::cout << "needle2 not foundn"; 
 
      return 0; 
    } 
    Output: 
    two 30s found at position 2 
    two 10s found at position 5 
    needle1 found at position 0 
    needle2 found at position 3 
*/  

?

11、?swap_ranges??

//将区间[first1,last1)内的元素与“从first2开始,个数相同”的元素相互交换  
//这两个序列可位于同一容器,或不同容器  
//如果第二序列小于第一序列长度,或者两序列在同一容器且重叠,则结果未可预期  
//Exchanges the values of each of the elements in the range [first1,last1)   
//with those of their respective elements in the range beginning at first2.  
template <class _ForwardIter1,class _ForwardIter2>  
_ForwardIter2 swap_ranges(_ForwardIter1 __first1,_ForwardIter2 __first2) {  
  __STL_REQUIRES(_ForwardIter1,_Mutable_ForwardIterator);  
  __STL_REQUIRES(_ForwardIter2,_Mutable_ForwardIterator);  
  __STL_CONVERTIBLE(typename iterator_traits<_ForwardIter1>::value_type,typename iterator_traits<_ForwardIter2>::value_type);  
  __STL_CONVERTIBLE(typename iterator_traits<_ForwardIter2>::value_type,typename iterator_traits<_ForwardIter1>::value_type);  
  for ( ; __first1 != __last1; ++__first1,++__first2)//遍历第一个序列  
    iter_swap(__first1,__first2);//交换迭代器所指的元素  
  return __first2;  
}  
//swap_ranges函数举例:  
/* 
    #include <iostream>     // std::cout 
    #include <algorithm>    // std::swap_ranges 
    #include <vector>       // std::vector 
 
    int main () { 
      std::vector<int> foo (5,10);        // foo: 10 10 10 10 10 
      std::vector<int> bar (5,33);        // bar: 33 33 33 33 33 
 
      std::swap_ranges(foo.begin()+1,foo.end()-1,bar.begin()); 
 
      // print out results of swap: 
      std::cout << "foo contains:"; 
      for (std::vector<int>::iterator it=foo.begin(); it!=foo.end(); ++it) 
        std::cout << ' ' << *it; 
      std::cout << 'n'; 
 
      std::cout << "bar contains:"; 
      for (std::vector<int>::iterator it=bar.begin(); it!=bar.end(); ++it) 
        std::cout << ' ' << *it; 
      std::cout << 'n'; 
 
      return 0; 
    } 
    Output: 
    foo contains: 10 33 33 33 10 
    bar contains: 10 10 10 33 33 
*/  

??

12、?transform??

第一个版本:以仿函数opr作用于[first,last)中的每一个元素,并以其结果产生出一个新的序列。

第二个版本:以仿函数binary_op作用于一双元素上(其中一个元素来自[first1,last1),另一个元素来自“从first2开始的序列”),并以其结果产生出一个新的序列。??

?

//两个版本  
/* 
函数原型: 
unary operation(1):版本一   
    template <class InputIterator,class OutputIterator,class UnaryOperation> 
    OutputIterator transform (InputIterator first1,InputIterator last1,OutputIterator result,UnaryOperation op); 
binary operation(2):版本二  
    template <class InputIterator1,class InputIterator2,class BinaryOperation> 
    OutputIterator transform (InputIterator1 first1,InputIterator1 last1,InputIterator2 first2,BinaryOperation binary_op); 
函数功能: 
(1) unary operation 
    Applies op to each of the elements in the range [first1,last1) and stores the value  
    returned by each operation in the range that begins at result. 
(2) binary operation 
    Calls binary_op using each of the elements in the range [first1,last1) as first argument,and the respective argument in the range that begins at first2 as second argument.  
    The value returned by each call is stored in the range that begins at result. 
*/  
//第一个版本:以仿函数opr作用于[first,last)中的每一个元素,并以其结果产生出一个新的序列  
template <class _InputIter,class _UnaryOperation>  
_OutputIter transform(_InputIter __first,_UnaryOperation __opr) {  
  __STL_REQUIRES(_InputIter,_OutputIterator);  
  
  for ( ; __first != __last; ++__first,++__result)  
    *__result = __opr(*__first);  
  return __result;  
}  
//第二个版本:以仿函数binary_op作用于一双元素上(其中一个元素来自[first1,last1),另一个元素来自“从first2开始的序列”)  
//并以其结果产生出一个新的序列  
template <class _InputIter1,class _InputIter2,class _BinaryOperation>  
_OutputIter transform(_InputIter1 __first1,_InputIter1 __last1,_InputIter2 __first2,_BinaryOperation __binary_op) {  
  __STL_REQUIRES(_InputIter1,_InputIterator);  
  __STL_REQUIRES(_InputIter2,_OutputIterator);  
  for ( ; __first1 != __last1; ++__first1,++__first2,++__result)  
    *__result = __binary_op(*__first1,*__first2);  
  return __result;  
}  
//transform函数举例:  
/*
  #include <iostream>
#include <algorithm>
    #include <vector>       // std::vector 
    #include <functional>   // std::plus 
 
    int op_increase (int i) { return ++i; } 
 
    int main () { 
      std::vector<int> foo; 
      std::vector<int> bar; 
 
      // set some values: 
      for (int i=1; i<6; i++) 
        foo.push_back (i*10);                         // foo: 10 20 30 40 50 
 
      bar.resize(foo.size());                         // allocate space 
 
      std::transform (foo.begin(),foo.end(),bar.begin(),op_increase); 
                                                      // bar: 11 21 31 41 51 
      std::cout << "bar contains:"; 
      for (std::vector<int>::iterator it=bar.begin(); it!=bar.end(); ++it) 
        std::cout << ' ' << *it; 
        std::cout << 'n'; 
 
      // std::plus adds together its two arguments: 
      std::transform (foo.begin(),foo.begin(),std::plus<int>()); 
                                                      // foo: 21 41 61 81 101 
 
      std::cout << "foo contains:"; 
      for (std::vector<int>::iterator it=foo.begin(); it!=foo.end(); ++it) 
        std::cout << ' ' << *it; 
      std::cout << 'n'; 
 
      return 0; 
    } 
    Output: 
    bar contains: 11 21 31 41 51 
    foo contains: 21 41 61 81 101 
*/  

?

13、unique_copy??

?将区间[first,last)内元素复制到以result开头的区间上,但是如果存在相邻重复元素时,只复制其中第一个元素???

/* 
函数原型: 
equality (1)     
    template <class InputIterator,class OutputIterator> 
    OutputIterator unique_copy (InputIterator first,InputIterator last,OutputIterator result); 
predicate (2)    
    template <class InputIterator,class BinaryPredicate> 
    OutputIterator unique_copy (InputIterator first,BinaryPredicate pred); 
*/  
//版本一  
template <class _InputIter,class _OutputIter>  
inline _OutputIter unique_copy(_InputIter __first,_OutputIter __result) {  
  __STL_REQUIRES(_InputIter,_OutputIterator);  
  __STL_REQUIRES(typename iterator_traits<_InputIter>::value_type,_EqualityComparable);  
  if (__first == __last) return __result;  
  //根据result迭代器的类型,调用不同的函数  
  return __unique_copy(__first,__result,__ITERATOR_CATEGORY(__result));  
}  
  
template <class _InputIter,class _BinaryPredicate,class _Tp>  
_OutputIter __unique_copy(_InputIter __first,_BinaryPredicate __binary_pred,_Tp*) {  
  __STL_BINARY_FUNCTION_CHECK(_BinaryPredicate,_Tp,_Tp);  
  _Tp __value = *__first;  
  *__result = __value;  
  while (++__first != __last)  
    if (!__binary_pred(__value,*__first)) {  
      __value = *__first;  
      *++__result = __value;  
    }  
  return ++__result;  
}  
  
template <class _InputIter,class _BinaryPredicate>  
inline _OutputIter __unique_copy(_InputIter __first,output_iterator_tag) {  
  return __unique_copy(__first,__binary_pred,__VALUE_TYPE(__first));  
}  
  
template <class _InputIter,class _ForwardIter,class _BinaryPredicate>  
_ForwardIter __unique_copy(_InputIter __first,_ForwardIter __result,forward_iterator_tag) {  
  __STL_BINARY_FUNCTION_CHECK(_BinaryPredicate,typename iterator_traits<_InputIter>::value_type);  
  *__result = *__first;  
  while (++__first != __last)  
    if (!__binary_pred(*__result,*__first)) *++__result = *__first;  
  return ++__result;  
}  
//版本二  
template <class _InputIter,class _BinaryPredicate>  
inline _OutputIter unique_copy(_InputIter __first,_BinaryPredicate __binary_pred) {  
  __STL_REQUIRES(_InputIter,_OutputIterator);  
  if (__first == __last) return __result;  
  //根据result迭代器的类型,调用不同的函数  
  return __unique_copy(__first,__ITERATOR_CATEGORY(__result));  
}  


14、unique

//移除区间[first,last)相邻连续重复的元素 ,只移除相邻的重复元素
//返回一个迭代器指向新区间的尾端,新区间不含相邻的重复元素,此算法稳定,即不改变原来元素的相对位置 
//unique有两个版本  
//功能:Removes all but the first element from every consecutive group of equivalent elements in the range [first,last).  
/* 
函数原型: 
equality (1):版本一采用operator==     
    template <class ForwardIterator> 
    ForwardIterator unique (ForwardIterator first,ForwardIterator last); 
predicate (2):版本二采用pred操作    
    template <class ForwardIterator,class BinaryPredicate> 
    ForwardIterator unique (ForwardIterator first,BinaryPredicate pred); 
*/  
//版本一  
template <class _ForwardIter>  
_ForwardIter unique(_ForwardIter __first,_Mutable_ForwardIterator);  
  __STL_REQUIRES(typename iterator_traits<_ForwardIter>::value_type,_EqualityComparable);  
  __first = adjacent_find(__first,__last);//找出第一个相邻元素的起始位置  
  return unique_copy(__first,__first);//调用unique_copy完成操作  
}  
//版本二  
template <class _ForwardIter,class _BinaryPredicate>  
_ForwardIter unique(_ForwardIter __first,_BinaryPredicate __binary_pred) {  
  __STL_REQUIRES(_ForwardIter,_Mutable_ForwardIterator);  
  __STL_BINARY_FUNCTION_CHECK(_BinaryPredicate,typename iterator_traits<_ForwardIter>::value_type);  
  __first = adjacent_find(__first,__binary_pred);//找出第一个相邻元素的起始位置  
  return unique_copy(__first,__first,__binary_pred);//调用unique_copy完成操作  
}  

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读