深入详解STL算法

来源:互联网 发布:淘宝引流的渠道有哪些 编辑:程序博客网 时间:2024/06/06 12:36

                    深入详解STL算法

    下面将介绍标准模板库定义的算法。这些算法通过迭代器在容器中进行操作。所有的算法都是模板函数。

算法详细说明:

    adjacent_find

    template<class _FwdIt> inline

        _FwdIt adjacent_find(_FwdIt _First, _FwdIt _Last);

    template<class _FwdIt, class _Pr> inline

        _FwdIt adjacent_find(_FwdIt _First, _FwdIt _Last, _Pr _Pred);

adjacent_find算法从startend的序列中搜索临近的匹配的元素,并返回第一个元素的迭代器。如果没有找到任何邻近对,将返回_last。上面第一种形式搜索等价元素。第二个形式允许指定自己的方法来确定元素是否匹配。

    binary_search

    template<class _FwdIt, class _Ty> inline

        bool binary_search(_FwdIt _First, _FwdIt _Last, const _Ty% _Val);

    template<class _FwdIt, class _Ty, class _Pr> inline

        bool binary_search(_FwdIt _First, _FwdIt _Last,const _Ty% _Val, _Pr _Pred);

该算法对已经排好序的序列从_fist_last范围内执行二分搜索,搜索值_val。如果找到了_val将返回true,否则,返回false。第一种形式比较指定序列中的元亲是否相等。第二钟形式可指定自己的比较函数。

    copy

    template<class _InIt, class _OutIt> inline

       _OutIt copy(_InIt _First, _InIt _Last, _OutIt _Dest);

copy()算法复制从_first开始到_last结束的序列.并将结果放入_DestBeg所指向的序列中,它返回指向结果序列结尾的指针,要复制的范围一定不能与_DestBeg重叠。返回迭代器为_DestEnd+(_Last-_First)

    count

    template<class _InIt, class _Ty> inline

       typename iterator_traits<_InIt>::difference_type

          count(_InIt _First, _InIt _Last, const _Ty% _Val);

返回序列中从_First处开始到_Last处结束范围内与_Val匹配的元素的个数。

    count_if  

    template<class _InIt, class _Pr> inline

       typename iterator_traits<_InIt>::difference_type

           count_if(_InIt _First, _InIt _Last, _Pr _Pred);

返回序列中从start处开始到end处结束范围内使一元谓词_Pred返回true的元素个数。

    equal      

    template<class _InIt1, class _InIt2> inline

      bool equal(_InIt1 _First1, _InIt1 _Last1, _InIt2 _First2);

    template<class _InIt1, class _InIt2, class _Pr> inline

      bool equal(_InIt1 _First1, _InIt1 _Last1, _InIt2 _First2, _Pr _Pred);

equal确定两个范围足否相同。算法将把_First1_Last1所决定的范围与_First2所指向的序列进行比较。如果两者相同,将返回true。否则,将返回false。第二种形式允许指定二元谓词来确定两个元素是否相等。

    equal_range

    template<class _FwdIt, class _Ty> inline

      _PAIR_TYPE(_FwdIt) equal_range(_FwdIt _First, _FwdIt _Last, const _Ty% _Val);

    template<class _FwdIt, class _Ty, class _Pr> inline

       _PAIR_TYPE(_FwdIt) equal_range(_FwdIt _First, _FwdIt _Last,const _Ty% _Val, _Pr _Pred);

该算法返回一个范围,在其中可插入元素到序列中而不会破坏排序顺序。_First_Last指定了搜索范围,搜索值以_Val传递进来。要指定自己的搜索条件,可以自己指定比较函数_Comp。模板类pair为应用类,能够在其firstsecond成员中保存一对对象。

    fill

    template<class _FwdIt, class _Ty> inline

      void fill(_FwdIt _First, _FwdIt _Last, const _Ty% _Val);

    fill_n

    template<class _OutIt, class _Diff, class _Ty> inline

       void fill_n(_OutIt _First, _Diff _Count, const _Ty% _Val);

fill()fill_n()算法使用val指定的值填充一个范围。对于fill(),范围是通过start

end指定的,而对于fill_n(),范围从_First开始,包含_Count个元素。

    find

    template<class _InIt, class _Ty> inline

       _InIt find(_InIt _First, _InIt _Last, const _Ty% _Val);

find()算法从 _First_Last的范围中搜索_Val值。它返回指向所找到的第一个元素的迭代器。如果没有找到这样的元素.将返问指向_Last的迭代器。

    find_end

      template<class _FwdIt1, class _FwdIt2> inline

       _FwdIt1 find_end(_FwdIt1 _First1, _FwdIt1 _Last1,_FwdIt2 _First2, _FwdIt2 _Last2);

     template<class _FwdIt1, class _FwdIt2, class _Pr> inline

       _FwdIt1 find_end(_FwdIt1 _First1, _FwdIt1 _Last1, _FwdIt2 _First2, _FwdIt2 _Last2, _Pr _Pred);

   该算法从[_First1, _Last1)范围内查找出[_First2, _Last2)定义的子序列。如果找到这样的序列,函数将返回指向范围中最后一个子序列中第一个一个元素的迭代器。否则,返回迭代器_Last1。第二种形式可指定确定元素何时匹配的二元谓词。

    find_first_of

    template<class _FwdIt1, class _FwdIt2> inline

      _FwdIt1 find_first_of(_FwdIt1 _First1, _FwdIt1 _Last1,_FwdIt2 _First2, _FwdIt2 _Last2);

    template<class _FwdIt1, class _FwdIt2, class _Pr> inline

      _FwdIt1 find_first_of(_FwdIt1 _First1, _FwdIt1 _Last1,_FwdIt2 _First2, _FwdIt2 _Last2, _Pr _Pred);

    find_if

    template<class _InIt, class _Pr> inline

       _InIt find_if(_InIt _First, _InIt _Last, _Pr _Pred);

[_First,_Last)的范围中搜索使一元谓词_Pred返回true的元素。它返回指向满足条件的第—个元素的选代器。如果序列中没有这样的值,算法将返回指向_Last的这代器。

    for_each

    template<class _InIt, class _Fn1> inline

       _Fn1 for_each(_InIt _First, _InIt _Last, _Fn1 _Func);

for_each 算法将范围 [first, last) 中的每个元素中调用函数 _Func,并返回输入的参数。

    generate

    template<class _FwdIt, class _Fn0> inline

       void generate(_FwdIt _First, _FwdIt _Last, _Fn0 _Func);

    generate_n

    template<class _OutIt, class _Diff, class _Fn0> inline

      void generate_n(_OutIt _Dest, _Diff _Count, _Fn0 _Func);

算法generategenerate_n为一个范围中的元素分配由生成器函数返回的值。对于generate(),范围_First_Lastd指定的。对于generate_n(),范围_First处开始,并包含了num个元素。生成器函数是通过_Gen传递进来的,它没有任何参数。

    includes

    template<class _InIt1, class _InIt2> inline

      bool includes(_InIt1 _First1, _InIt1 _Last1,_InIt2 _First2, _InIt2 _Last2);

    template<class _InIt1, class _InIt2, class _Pr> inline

      bool includes(_InIt1 _First1, _InIt1 _Last1,_InIt2 _First2, _InIt2 _Last2, _Pr _Pred);

includes用来确定由_First1_Last1定义的已排好序的序列是否包括由_First2_Last2所定义的已排好序的序列中的所有元素。如果都包括,它返回true,否则算法返回false。在第二种形式中,可指定确定两个元素大小关系的比较函数。

   iter_swap 

   template<class _FwdIt1, class _FwdIt2> inline

      void iter_swap(_FwdIt1 _Left, _FwdIt2 _Right);

将它的两个迭代器参数指向的值进行交换。

lexicographical_compare

template<class _InIt1, class _InIt2> inline

    bool lexicographical_compare(_InIt1 _First1, _InIt1 _Last1,_InIt2 _First2, _InIt2 _Last2);

template<class _InIt1, class _InIt2, class _Pr> inline

    bool lexicographical_compare(_InIt1 _First1, _InIt1 _Last1,_InIt2 _First2, _InIt2 _Last2, _Pr _Pred);

按字母表顺序将_First1_Last1定义的序列_First2_Last22所定义的序列进行比较。如果第—个序列按字典顺厅来说小丁第二个(换句话说.如果按字典顺序第一个序列在第二个之前).则返回true。在第二种形式中,可指定确定两个元素大小关系的比较函数。

    lower_bound   

    template<class _FwdIt, class _Ty> inline

       _FwdIt lower_bound(_FwdIt _First, _FwdIt _Last, const _Ty% _Val);

    template<class _FwdIt, class _Ty, class _Pr> inline

       _FwdIt lower_bound(_FwdIt _First, _FwdIt _Last,const _Ty% _Val, _Pr _Pred);

该算法在startend定义的序列中查找值<=val的第一个元素。它返回指向此点的迭代器。在第二种形式中,可指定确定两个元素大小关系的比较函数。

    max    

    template<class _Ty> inline

      const _Ty max(const _Ty% _Left, const _Ty% _Right);

    template<class _Ty, class _Pr> inline

      const _Ty max(const _Ty% _Left, const _Ty% _Right, _Pr _Pred);

返回两个数中最大的一个值 。在第二种形式中,可指定确定两个元素大小关系的比较函数。

   max_element

   template<class _FwdIt> inline

      _FwdIt max_element(_FwdIt _First, _FwdIt _Last);

   template<class _FwdIt, class _Pr> inline

      _FwdIt max_element(_FwdIt _First, _FwdIt _Last, _Pr _Pred);

返回指向从startend的范围中最大元素的选代器。在第二种形式中,可指定确定两个元素大小关系的比较函数。

   merge

   template<class _InIt1, class _InIt2, class _OutIt> inline

     _OutIt merge(_InIt1 _First1, _InIt1 _Last1,_InIt2 _First2, _InIt2 _Last2, _OutIt _Dest);

   template<class _InIt1, class _InIt2, class _OutIt, class _Pr> inline

      _OutIt merge(_InIt1 _First1, _InIt1 _Last1, _InIt2 _First2, _InIt2 _Last2, _OutIt _Dest, _Pr _Pred);

merge算法将两个已排序的序列合并起来,并将结果放入第三个序列中。要合并的序列是通过_First1_Last1_First2_Last2定义的,结果放入_Result指向的序列。算法返回指向结果序列结尾的迭代器。在第二种形式中,可指定确定两个元素大小关系的比较函数。

    next_permutation

    template<class _BidIt> inline

      bool next_permutation(_BidIt _First, _BidIt _Last);

    template<class _BidIt, class _Pr> inline

      bool next_permutation(_BidIt _First, _BidIt _Last, _Pr _Pred);

该算法构造一个序列的下一个排列。在生成此排列时,假定从低到高有序的序列代表第一个排列。如果下一个排列不存在,该函数将对序列进行排序,使其变为相应的第一个排列,并返回false。否则,算法返回true。在第二种形式中,可指定确定两个元素大小关系的比较函数。

    nth_element

    template<class _RanIt> inline

       void nth_element(_RanIt _First, _RanIt _Nth, _RanIt _Last);

    template<class _RanIt, class _Pr> inline

      void nth_element(_RanIt _First, _RanIt _Nth, _RanIt _Last, _Pr _Pred);

该算法对从_First_Last的序列进行排列,使所有小于_Nth的元索都位于_Nth之前,所有大于_Nth的元素都位丁_Nth之后。在第二种形式中,可指定确定两个元素大小关系的比较函数。

    partial_sort

    template<class _RanIt> inline

      void partial_sort(_RanIt _First, _RanIt _Mid, _RanIt _Last);

    template<class _RanIt, class _Pr> inline

      void partial_sort(_RanIt _First, _RanIt _Mid, _RanIt _Last,_Pr _Pred);

该算法对_First_Last定义的序列进行排列,但是,在执行此算法之后,只有从First_SortEnd范围内的元素是有序的。在第二种形式中,可指定确定两个元素大小关系的比较函数。

    partial_sort_copy

    template<class _InIt, class _RanIt> inline

      _RanIt partial_sort_copy(_InIt _First1, _InIt _Last1,_RanIt _First2, _RanIt _Last2);

    template<class _InIt, class _RanIt, class _Pr> inline

      _RanIt partial_sort_copy(_InIt _First1, _InIt _Last1,_RanIt _First2, _RanIt _Last2, _Pr _Pred);

该算法对_First1_Last1的范围进行排序,并复制尽量多的元素到_First2_Last2定义的结果序列中,它返回指向复制到结果序列中最后—个元素的迭代器。在第二种形式中,可指定确定两个元素大小关系的比较函数。

    partion

    template<class _BidIt, class _Pr> inline

      _BidIt partition(_BidIt _First, _BidIt _Last, _Pr _Pred);

partion()算法对_First_Last定义的序列进行排列,这样,所有使_Comph所指定谓词返回true的元素均位于所有使此谓词返回false的元素之前。它返回指向第一个使指定谓词为false的元素的迭代器。

    prev_permutation

    template<class _BidIt> inline

       bool prev_permutation(_BidIt _First, _BidIt _Last);

    template<class _BidIt, class _Pr> inline

       bool prev_permutation(_BidIt _First, _BidIt _Last, _Pr _Pred);

该算法构造一个序列的上一个排列。在生成此排列时,假定从低到高有序的序列代表第一个排列。如果上一个排列不存在,该函数将对序列进行排序,使其变为最后一个排列,并返回false。否则,算法返回true。在第二种形式中,可指定确定两个元素大小关系的比较函数。

    random_shuffle

    template<class _RanIt> inline

      void random_shuffle(_RanIt _First, _RanIt _Last);

    template<class _RanIt, class _Fn1> inline

      void random_shuffle(_RanIt _First, _RanIt _Last, _Fn1% _Func);

示例:

#include <ctime>

int   myrand(int   n)  

  {  

      srand((int)time(0));

          return   rand()   %   n;          

  }  

int main( ) {

   using namespace std;

   vector <int> v1;

   vector <int>::iterator Iter1, Iter2;

 

   int i;

   for ( i = 1 ; i <= 9 ; i++ )

      v1.push_back( i );

 

   random_shuffle( v1.begin( ), v1.end( ));

   cout << "The original version of vector v1 is: ( " ;

   for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )

      cout << *Iter1 << " ";

   cout << ")." << endl;

 

   // Shuffled once

   random_shuffle( v1.begin( ), v1.end( ),myrand);

 

   cout << "Vector v1 after one shuffle is:       ( " ;

   for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )

      cout << *Iter1 << " ";

   cout << ")." << endl;

}

输出结果:

The original version of vector v1 is: ( 9 2 7 3 1 6 8 4 5 ).

Vector v1 after one shuffle is:       ( 1 9 6 7 2 5 4 8 3 ).

    remove

    template<class _FwdIt, class _Ty> inline

      _FwdIt remove(_FwdIt _First, _FwdIt _Last, const _Ty% _Val);

    remove_if

    template<class _FwdIt, class _Pr> inline

       _FwdIt remove_if(_FwdIt _First, _FwdIt _Last, _Pr _Pred);

    remove_copy

    template<class _InIt, class _OutIt, class _Ty> inline

      _OutIt remove_copy(_InIt _First, _InIt _Last,_OutIt _Dest, const _Ty% _Val);

    remove_copy_if

    template<class _InIt, class _OutIt, class _Pr> inline

      _OutIt remove_copy_if(_InIt _First, _InIt _Last, _OutIt _Dest,_Pr _Pred);

1.remove()从指定范围中删除值等于val的元素,它返回指向剩余元素结尾的迭代器。

2.remove_if从指定范围中删除使谓词_Pred返回true的元素,它返回指向剩余元素结尾的迭代器。

3.remove_copy从指定范围中复制值等丁val的元素,并将结果放入result所指向的序列中:它返回指向结果序列结尾的迭代器。

4.remove_copy_if从指定范围中复制使谓词_Pred返回true的元素,并将结果放入result所指向的序列中:它返回指向结果序列结尾的迭代器。

    replace

    template<class ForwardIterator, class Type>

    template<class _FwdIt, class _Ty> inline

    void replace(_FwdIt _First, _FwdIt _Last,const _Ty% _Oldval, const _Ty% _Newval);

    replace_if

    template<class _FwdIt, class _Pr, class _Ty> inline

     void replace_if(_FwdIt _First, _FwdIt _Last, _Pr _Pred,const _Ty% _Val);

    replace_copy

    template<class _InIt, class _OutIt, class _Ty> inline

      _OutIt replace_copy(_InIt _First, _InIt _Last, _OutIt _Dest, const _Ty% _Oldval, const _Ty% _Newval);

    replace_copy_if

    template<class _InIt, class _OutIt, class _Pr, class _Ty> inline

      _OutIt replace_copy_if(_InIt _First, _InIt _Last, _OutIt _Dest,_Pr _Pred, const _Ty% _Val);

1.在指定的范围中,replace使用值为new的元素替换值为old的元素。

2.在指定的范围中,replace_if使用值为new的元素替换使谓词_Pred返回true的元素。

3.在指定的范围中,replace_copy复制元素到_Result中。在此过程中,它使用值为new的元素替换值为01d的元素。原始范围保持不变。算法返回指向_Result结尾的迭代器。

4.在指定的范围中,replace_copy_if复制元素到_Result中。在此过程中,它使用值为new的元素替换使谓词_Pred返回true的元素。原始范围保持不变。算法返回指向_Result结尾的迭代器。

    reverse

    template<class _BidIt> inline

      void reverse(_BidIt _First, _BidIt _Last);

    reverse_copy

    template<class _BidIt, class _OutIt> inline

       _OutIt reverse_copy(_BidIt _First, _BidIt _Last, _OutIt _Dest);

1.reverse()算法反转由_First_Last所指定范围的顺序。

2.reverse_copy()算法以逆序复制_First_Last所指定的范围,并将结果放入_Result中。它返回指_Result结尾的迭代器。

    rotate

    template<class _FwdIt> inline

      void rotate(_FwdIt _First, _FwdIt _Mid, _FwdIt _Last);

    rotate_copy

    template<class _FwdIt, class _OutIt> inline

      _OutIt rotate_copy(_FwdIt _First, _FwdIt _Mid, _FwdIt _Last,_OutIt _Dest);

1.rotate()_First_Last所指定范围中的元素向左旋转,使mid指定的元素成为新的首元素。

2.rotate_copy()复制_First_Last所指定的范围,并将结果存储到_Result中,在此过程中,它会将元素向左旋转,使得mid指定的元素成为新的首元素。它返回指向_Result结尾的迭代器。

    search

    template<class _FwdIt1, class _FwdIt2> inline

      _FwdIt1 search(_FwdIt1 _First1, _FwdIt1 _Last1,_FwdIt2 _First2, _FwdIt2 _Last2);

    template<class _FwdIt1, class _FwdIt2, class _Pr> inline

      _FwdIt1 search(_FwdIt1 _First1, _FwdIt1 _Last1,_FwdIt2 _First2, _FwdIt2 _Last2, _Pr _Pred);

search()搜索序列的子序列。被搜索的序列由_First1_Last1定义,而要搜索的子序列则由_First2_Last2指定。如果找到了这样的子序列,将返指向其开头的迭代器。否则,返回_Last1。在第二种形式中,可指定确定两个元素何时相等的二元谓词。

    search_n

    template<class _FwdIt1, class _Diff2, class _Ty> inline

      _FwdIt1 search_n(_FwdIt1 _First1, _FwdIt1 _Last1,_Diff2 _Count, const _Ty& _Val);

    template<class _FwdIt1, class _Diff2, class _Ty, class _Pr> inline

      _FwdIt1 search_n(_FwdIt1 _First1, _FwdIt1 _Last1,_Diff2 _Count, const _Ty& _Val, _Pr _Pred);

search_n是从一个[_first1, _last1)范围的数据中查找有_Count个连续的数据,每个数据都和_Val相同。如果找到,它将返回查找到的第一个数据的迭代指针,如果找不到,则返回_last1 。在第二种形式中,可指定确定两个元素何时相等的二元谓词。

    set_difference

    template<class _InIt1, class _InIt2, class _OutIt> inline

      _OutIt set_difference(_InIt1 _First1, _InIt1 _Last1,_InIt2 _First2, _InIt2 _Last2,_OutIt _Dest);

    template<class _InIt1, class _InIt2, class _OutIt, class _Pr> inline

     _OutIt set_difference(_InIt1 _First1, _InIt1 _Last1,_InIt2 _First2, _InIt2 _Last2, _OutIt _Dest, _Pr _Pred);

该算法生成包含两个有序的集合之差的序列。这两个集合分别由_First1_Last1_First2_Last2定义。换句话说,要将_First2_Last2定义的集合从_First1_Last1所定义的集合中减掉。结果是有序的,并放_Result中。算法返回指向_Result结尾的迭代器。第二种形式中可指定决定两个元素大小关系的比较函数。

注意:两个序列必须是有序的。

    set_intersection

    template<class _InIt1, class _InIt2, class _OutIt> inline

     _OutIt set_intersection(_InIt1 _First1, _InIt1 _Last1,_InIt2 _First2, _InIt2 _Last2, _OutIt _Dest);

    template<class _InIt1, class _InIt2, class _OutIt, class _Pr> inline

     _OutIt set_intersection(_InIt1 _First1, _InIt1 _Last1,_InIt2 _First2, _InIt2 _Last2, _OutIt _Dest, _Pr _Pred);

生成包含两个有序集合的交集的序列。这两个集合分别由_First1_Last1_First2_Last2定义。结呆序列包含了两个集合共有的元案。结果是有序的,并放_Result中。算法返回指向_Result结尾的迭代器。第二种形式中可指定决定两个元素大小关系的比较函数。

注意:两个序列必须是有序的。

    set_symmetric_difference

    template<class _InIt1, class _InIt2, class _OutIt> inline

     _OutIt set_symmetric_difference(_InIt1 _First1, _InIt1 _Last1,_InIt2 _First2, _InIt2 _Last2, _OutIt _Dest);

    template<class _InIt1, class _InIt2, class _OutIt, class _Pr> inline

      _OutIt set_symmetric_difference(_InIt1 _First1, _InIt1 _Last1,_InIt2 _First2, _InIt2 _Last2, _OutIt _Dest, _Pr _Pred);

该算法生成包含两个有序集合对称差的序列。这两个集合分别由_First1_Last1_First2_Last2定义。换句话说,。结果集合只包含不为两个集合共有的元素。结果是有序的,并放_Result中。算法返回指向_Result结尾的迭代器。第二种形式中可指定决定两个元素大小关系的比较函数。

注意:两个序列必须是有序的。

    set_union

    template<class _InIt1, class _InIt2, class _OutIt> inline

     _OutIt set_union(_InIt1 _First1, _InIt1 _Last1,_InIt2 _First2, _InIt2 _Last2, _OutIt _Dest);

    template<class _InIt1, class _InIt2, class _OutIt, class _Pr> inline

     _OutIt set_union(_InIt1 _First1, _InIt1 _Last1, _InIt2 _First2, _InIt2 _Last2, _OutIt _Dest, _Pr _Pred);

该算法生成包含两个有序集合的并集的序列。这两个集合分别由_First1_Last1_First2_Last2定义。结呆序列包含了两个集合中的所有元案。结果是有序的,并放_Result中。算法返回指向_Result结尾的迭代器。第二种形式中可指定决定两个元素大小关系的比较函数。

注意:两个序列必须是有序的。

    sort

    template<class _RanIt> inline

      void sort(_RanIt _First, _RanIt _Last);

    template<class _RanIt, class _Pr> inline

      void sort(_RanIt _First, _RanIt _Last, _Pr _Pred);

sort()算法对_First_Last指定的范围进行排序。第二种形式中可指定决定两个元素大小关系的比较函数。

    stable_partition

    template<class _BidIt, class _Pr> inline

      _BidIt stable_partition(_BidIt _First, _BidIt _Last, _Pr _Pred);

该算法对_First_Last定义的序列进行排列,使得所有使_Pred谓词返回true的元素.均位于便谓词返回false的元素之前。划分是稳定的,这表示序列的相对顺序是预定的。算法返回指向使谓词返回false的第一个元素的迭代器。

    stable_sort

    template<class _BidIt> inline

       void stable_sort(_BidIt _First, _BidIt _Last);

    template<class _BidIt, class _Pr> inline

      void stable_sort(_BidIt _First, _BidIt _Last, _Pr _Pred);

算法对_First_Last指定的范围进行排序,但排序是稳定的,这表示相等的元素不会重新排列。第二种形式中可指定决定两个元素大小关系的比较函数。

    swap

    template<class Type>

     void swap(Type& _Left, Type& _Right);

交换由_Left_Right引用的值。

    swap_ranges

    template<class _FwdIt1, class _FwdIt2> inline

     _FwdIt2 swap_ranges(_FwdIt1 _First1, _FwdIt1 _Last1,_FwdIt2 _First2);

该算法将_First1_Last1所指定范围中的元素与序列中从_First2处开始的元素相互交换,它返回指向_First2所指定序列结尾的指针。注意,对于交换部分的元素,First1_Last1所指定范围中的元素个数必须不大于从_First2处开始的元素个数。

    transform

    template<class _InIt, class _OutIt, class _Fn1> inline

     _OutIt transform(_InIt _First, _InIt _Last, _OutIt _Dest,_Fn1 _Func);

    template<class _InIt1, class _InIt2, class _OutIt, class _Fn2> inline

     _OutIt transform(_InIt1 _First1, _InIt1 _Last1, _InIt2 _First2,_OutIt _Dest, _Fn2 _Func);

该算法将一个函数应用到元素范围,并将结果存储到_Result中,在第一种形式中.范围是由_First1_Last1指定的.所应用的函数由_Func指定。这个函数使用自己的参数接收一个元素的值,并必须返回元素的转换结果。

    在第二钟形式中,转换是通过二元运算符函数执行的,函数使用第一个参数从要转换的序列中接收一个元素的值,并用第二个参数接收第二个序列中的—个元素。

    两个版本都返回指向结果序列结尾的迭代器。

示例:

// alg_transform.cpp

// compile with: /EHsc

#include <vector>

#include <algorithm>

#include <functional>

#include <iostream>

 

// The function object multiplies an element by a Factor

template <class Type>

class MultValue

{

   private:

      Type Factor;   // The value to multiply by

   public:

      // Constructor initializes the value to multiply by

      MultValue ( const Type& _Val ) : Factor ( _Val ) {

      }

 

      // The function call for the element to be multiplied

      Type operator ( ) ( Type& elem ) const

      {

         return elem * Factor;

      }

};

 

int main( )

{

   using namespace std;

   vector <int> v1, v2 ( 7 ), v3 ( 7 );

   vector <int>::iterator Iter1, Iter2 , Iter3;

 

   // Constructing vector v1

   int i;

   for ( i = -4 ; i <= 2 ; i++ )

   {

      v1.push_back(  i );

   }

 

   cout << "Original vector  v1 = ( " ;

   for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )

      cout << *Iter1 << " ";

   cout << ")." << endl;

 

   // Modifying the vector v1 in place

   transform (v1.begin ( ) , v1.end ( ) , v1.begin ( ) , MultValue<int> ( 2 ) );

   cout << "The elements of the vector v1 multiplied by 2 in place gives:"

        << "/n v1mod = ( " ;

   for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )

      cout << *Iter1 << " ";

   cout << ")." << endl;

 

   // Using transform to multiply each element by a factor of 5

   transform ( v1.begin ( ) , v1.end ( ) , v2.begin ( ) , MultValue<int> ( 5 ) );

  

   cout << "Multiplying the elements of the vector v1mod/n "

        <<  "by the factor 5 & copying to v2 gives:/n v2 = ( " ;

   for ( Iter2 = v2.begin( ) ; Iter2 != v2.end( ) ; Iter2++ )

      cout << *Iter2 << " ";

   cout << ")." << endl;

 

   // The second version of transform used to multiply the

   // elements of the vectors v1mod & v2 pairwise

   transform ( v1.begin ( ) , v1.end ( ) ,  v2.begin ( ) , v3.begin ( ) ,

      multiplies <int> ( ) );

  

   cout << "Multiplying elements of the vectors v1mod and v2 pairwise "

        <<  "gives:/n v3 = ( " ;

   for ( Iter3 = v3.begin( ) ; Iter3 != v3.end( ) ; Iter3++ )

      cout << *Iter3 << " ";

   cout << ")." << endl;

}

输出结果:

Original vector  v1 = ( -4 -3 -2 -1 0 1 2 ).

The elements of the vector v1 multiplied by 2 in place gives:

 v1mod = ( -8 -6 -4 -2 0 2 4 ).

Multiplying the elements of the vector v1mod

 by the factor 5 & copying to v2 gives:

 v2 = ( -40 -30 -20 -10 0 10 20 ).

Multiplying elements of the vectors v1mod and v2 pairwise gives:

 v3 = ( 320 180 80 20 0 20 80 ).

注意:v2.size()>=v1.size();

    unique  

    template<class _FwdIt> inline

      _FwdIt unique(_FwdIt _First, _FwdIt _Last);

    template<class _FwdIt, class _Pr> inline

       _FwdIt unique(_FwdIt _First, _FwdIt _Last, _Pr _Pred);

该算法从指定范围删除连续的重复元素。在第二种形式中,可指定确定两个元素何时相等的二元谓词。unique返回指向范围结尾的迭代器。  

    unique_copy

    template<class _InIt, class _OutIt> inline

       _OutIt unique_copy(_InIt _First, _InIt _Last, _OutIt _Dest);

    template<class _InIt, class _OutIt, class _Pr> inline

      _OutIt unique_copy(_InIt _First, _InIt _Last, _OutIt _Dest,_Pr _Pred);

该算法复制由_First_Last指定的范围,并删除其中的连续重复元素。结果放入_Result中。在第二种形式中,可指定确定两个元素何时相等的二元谓词。算法返回指向范围结尾的迭代器。

    upper_bound

    template<class _FwdIt, class _Ty> inline

      _FwdIt upper_bound(_FwdIt _First, _FwdIt _Last, const _Ty% _Val);

    template<class _FwdIt, class _Ty, class _Pr> inline

      _FwdIt upper_bound(_FwdIt _First, _FwdIt _Last,const _Ty% _Val, _Pr _Pred);

该算法在_First_Last定义的序列中查找大于_Val的第一个元素的位置.它返回指向此位置的迭代器,在第二种形式中,可指定确定两个元素大小关系的比较函数。

原创粉丝点击