zoukankan      html  css  js  c++  java
  • 第22章 变易算法

     

    第22章 变易算法  Modifying  sequence operations


       22.1 元素复制copy
    copy  Copy range of elements (function template)   
       22.2 反向复制copy_backward
    copy_backward  Copy range of elements backwards (function template)   
       22.3 元素交换swap
    swap  Exchange values of two objects (function template)   
       22.4 迭代器交换iter_swap
    iter_swap  Exchange values of objects pointed by two iterators (function template)   
       22.5 区间元素交换swap_ranges
    swap_ranges  Exchange values of two ranges (function template)   
       22.6 元素变换transform
    transform  Apply function to range (function template)   
       22.7 替换Replace
    replace  Replace value in range (function template)   
       22.8 条件替换replace_if
    replace_if  Replace values in range (function template)   
       22.9 替换和复制replace_copy
    replace_copy  Copy range replacing value (function template)   
       22.10 条件替换和复制replace_copy_if
    replace_copy_if  Copy range replacing value (function template)   
       22.11 填充fill
    fill  Fill range with value (function template)   
       22.12 n次填充fill_n
    fill_n  Fill sequence with value (function template)   
       22.13 随机生成元素generate
    generate  Generate values for range with function (function template)   
       22.14 随机生成n个元素generate_n
    generate_n  Generate values for sequence with function (function template)   
       22.15 移除复制remove_copy
    remove_copy  Copy range removing value (function template)
       22.16 条件移除复制remove_copy_if
    remove_copy_if  Copy range removing values (function template)
       22.17 移除remove
    remove  Remove value from range (function template)
       22.18 条件移除remove_if
    remove_if  Remove elements from range (function template)
       22.19 不连续重复元素复制unique_copy
    unique_copy  Copy range removing duplicates (function template)
       22.20 剔除连续重复元素unique
    unique  Remove consecutive duplicates in range (function template)
       22.21 元素反向reverse
    reverse  Reverse range (function template)
       22.22 反向复制reverse_copy
    reverse_copy  Copy range reversed (function template)
       22.23 旋转rotate
    rotate  Rotate elements in range (function template)
       22.24 旋转复制rotate_copy
    rotate_copy  Copy rotated range (function template)
       22.25 随机抖动random_shuffle
    random_shuffle  Rearrangle elements in range randomly (function template)   
       22.26 随机采样random_sample
    ...
       22.27 容器分割partition
    partition  Partition range in two (function template)   
       22.28 容器稳定分割stable_partition
    stable_partition  Divide range in two groups - stable ordering (function template)   
       22.29 本章小结

     

    /*
    
      第22章 变易算法
       22.1 元素复制copy
       22.2 反向复制copy_backward
       22.3 元素交换swap
       22.4 迭代器交换iter_swap
       22.5 区间元素交换swap_ranges
       22.6 元素变换transform
       22.7 替换Replace
       22.8 条件替换replace_if
       22.9 替换和复制replace_copy
       22.10 条件替换和复制replace_copy_if
       22.11 填充fill
       22.12 n次填充fill_n
       22.13 随机生成元素generate
       22.14 随机生成n个元素generate_n
       22.15 移除复制remove_copy
       22.16 条件移除复制remove_copy_if
       22.17 移除remove
       22.18 条件移除remove_if
       22.19 不连续重复元素复制unique_copy
       22.20 剔除连续重复元素unique
       22.21 元素反向reverse
       22.22 反向复制reverse_copy
       22.23 旋转rotate
       22.24 旋转复制rotate_copy
       22.25 随机抖动random_shuffle
       22.26 随机采样random_sample
       22.27 容器分割partition
       22.28 容器稳定分割stable_partition
       22.29 本章小结
    
    
    
      第22章 变易算法
    Modifying  sequence operations:  
       22.1 元素复制copy
    copy  Copy range of elements (function template)   
       22.2 反向复制copy_backward
    copy_backward  Copy range of elements backwards (function template)   
       22.3 元素交换swap
    swap  Exchange values of two objects (function template)   
       22.4 迭代器交换iter_swap
    iter_swap  Exchange values of objects pointed by two iterators (function template)   
       22.5 区间元素交换swap_ranges
    swap_ranges  Exchange values of two ranges (function template)   
       22.6 元素变换transform
    transform  Apply function to range (function template)   
       22.7 替换Replace
    replace  Replace value in range (function template)   
       22.8 条件替换replace_if
    replace_if  Replace values in range (function template)   
       22.9 替换和复制replace_copy
    replace_copy  Copy range replacing value (function template)   
       22.10 条件替换和复制replace_copy_if
    replace_copy_if  Copy range replacing value (function template)   
       22.11 填充fill
    fill  Fill range with value (function template)   
       22.12 n次填充fill_n
    fill_n  Fill sequence with value (function template)   
       22.13 随机生成元素generate
    generate  Generate values for range with function (function template)   
       22.14 随机生成n个元素generate_n
    generate_n  Generate values for sequence with function (function template)   
       22.15 移除复制remove_copy
    remove_copy  Copy range removing value (function template)
       22.16 条件移除复制remove_copy_if
    remove_copy_if  Copy range removing values (function template)
       22.17 移除remove
    remove  Remove value from range (function template)
       22.18 条件移除remove_if
    remove_if  Remove elements from range (function template)
       22.19 不连续重复元素复制unique_copy
    unique_copy  Copy range removing duplicates (function template)
       22.20 剔除连续重复元素unique
    unique  Remove consecutive duplicates in range (function template)
       22.21 元素反向reverse
    reverse  Reverse range (function template)
       22.22 反向复制reverse_copy
    reverse_copy  Copy range reversed (function template)
       22.23 旋转rotate
    rotate  Rotate elements in range (function template)
       22.24 旋转复制rotate_copy
    rotate_copy  Copy rotated range (function template)
       22.25 随机抖动random_shuffle
    random_shuffle  Rearrangle elements in range randomly (function template)   
       22.26 随机采样random_sample
    ...
       22.27 容器分割partition
    partition  Partition range in two (function template)   
       22.28 容器稳定分割stable_partition
    stable_partition  Divide range in two groups - stable ordering (function template)   
       22.29 本章小结
    
    
    
    */
    View Code

     

      第22章 变易算法
    Modifying  sequence operations: 
       22.1 元素复制copy
    copy  Copy range of elements (function template)  

    //  第22章 变易算法
    
    //   22.1 元素复制copy ---------------------------------------------------
    
    template<class InputIterator, class OutputIterator>
      OutputIterator copy ( InputIterator first, InputIterator last, OutputIterator result )
    {
      while (first!=last) *result++ = *first++;
      return result;
    }
    
    // 把序列一中某范围内的元素,复制到序列二中去。前两个参数是序列一范围,第三个参数是序列二的开始位置
    // 当序列二不够大时,不够装部分将复制不过去。
    
    
    // copy algorithm example
    #include <iostream>
    #include <algorithm>
    #include <vector>
    using namespace std;
    
    int main () {
      int myints[]={10,20,30,40,50,60,70};
      vector<int> myvector;
      vector<int>::iterator it;
    
      myvector.resize(7);   // allocate space for 7 elements
                            // 如7改成5,将只复制前5个元素
      copy ( myints, myints+7, myvector.begin() );
    
      cout << "myvector contains:";
      for (it=myvector.begin(); it!=myvector.end(); ++it)
        cout << " " << *it;
    
      cout << endl;
    
      return 0;
    }
    
    
    // 304
    #include <algorithm>
    #include <vector>
    #include <list>
    #include <iostream>
    using namespace std;
    void print(int x)
    {
      cout << x << "  ";
    }
    int main(void)
    {
      //初始化向量v
      vector < int > v;
      v.push_back(1);
      v.push_back(3);
      v.push_back(5);
      //初始化双向链表l
      list < int > l;
      l.push_back(2);
      l.push_back(4);
      l.push_back(6);
      l.push_back(8);
      l.push_back(10);
      //复制v到l
      copy(v.begin(), v.end(), l.begin());
      //链表l打印l 3 5 8 10
      for_each(l.begin(), l.end(), print);
      cout << endl;
      return 0;
    }


       22.2 反向复制copy_backward
    copy_backward  Copy range of elements backwards (function template)  

    //   22.2 反向复制copy_backward ---------------------------------------------------
    
    template<class BidirectionalIterator1, class BidirectionalIterator2>
      BidirectionalIterator2 copy_backward ( BidirectionalIterator1 first,
                                             BidirectionalIterator1 last,
                                             BidirectionalIterator2 result )
    {
      while (last!=first) *(--result) = *(--last);
      return result;
    }
    
    // 把序列一中某范围内的元素,复制到序列二中去。前两个参数是序列一范围,第三个参数是序列二的结束位置的下一个位置。
    // 两序列可以是同一序列
    
    // copy_backward example
    #include <iostream>
    #include <algorithm>
    #include <vector>
    using namespace std;
    
    int main () {
      vector<int> myvector;
      vector<int>::iterator it;
    
      // set some values:
      for (int i=1; i<=5; i++)
        myvector.push_back(i*10);          // myvector: 10 20 30 40 50
    
      myvector.resize(myvector.size()+3);  // allocate space for 3 more elements
    
      copy_backward ( myvector.begin(), myvector.begin()+5, myvector.end() );
    
      cout << "myvector contains:";
      for (it=myvector.begin(); it!=myvector.end(); ++it)
        cout << " " << *it;
    
      cout << endl;
    
      return 0;
    }
    
    
    //306 始终是左闭右开区间操作
    #include <algorithm>
    #include <vector>
    #include <iostream>
    using namespace std;
    
    void print(int x)
    {
      cout << x << ' ';
    }
    
    int main(void)
    {
      vector < int > v(10);
      for(unsigned int i = 0; i < v.size(); i++)
        v[i] = i + 1;  // 1 2 3 4 5 6 7 8 9 10
      copy_backward(v.begin(), v.begin() + 3, v.end());
      for_each(v.begin(), v.end(), print);
      cout << endl;    // 1 2 3 4 5 6 7 1 2 3
      return 0;
    }


       22.3 元素交换swap
    swap  Exchange values of two objects (function template)  

    //   22.3 元素交换swap ---------------------------------------------------
    
    template <class T> void swap ( T& a, T& b )
    {
      T c(a); a=b; b=c;
    }
    // 不通过指针交换两个元素
    
    
    // swap algorithm example。只要是同一类型的,统统交换
    #include <iostream>
    #include <algorithm>
    #include <vector>
    using namespace std;
    
    int main () {
    
      int x=10, y=20;                         // x:10 y:20
      swap(x,y);                              // x:20 y:10
    
      vector<int> first (4,x), second (6,y);  // first:4x20 second:6x10
      swap(first,second);                     // first:6x10 second:4x20
    
      cout << "first contains:";
      for (vector<int>::iterator it=first.begin(); it!=first.end(); ++it)
        cout << " " << *it;
    
      cout << endl;
    
      return 0;
    }
    
    
    
    //307
    #include <algorithm>
    #include <iostream>
    int main(void)
    {
      using namespace std;
      int a = 5;
      int b = 26;
      cout << "交换前 " << "a = " << a << " b = " << b << endl;
      swap(a, b);
      cout << "交换后 " << "a = " << a << " b = " << b << endl;
      return 0;
    }


       22.4 迭代器交换iter_swap
    iter_swap  Exchange values of objects pointed by two iterators (function template)  

    //   22.4 迭代器交换iter_swap ---------------------------------------------------
    
    template <class ForwardIterator1, class ForwardIterator2>
      void iter_swap ( ForwardIterator1 a, ForwardIterator2 b )
    {
      swap (*a, *b);
    }
    // 利用指针(迭代器)交换值
    
    
    
    // iter_swap example
    #include <iostream>
    #include <algorithm>
    #include <vector>
    using namespace std;
    
    int main () {
    
      int myints[]={10,20,30,40,50 };          //   myints:  10  20  30  40  50
      vector<int> myvector (4,99);             // myvector:  99  99  99  99
    
      iter_swap(myints,myvector.begin());      //   myints: [99] 20  30  40  50
                                               // myvector: [10] 99  99  99
    
      iter_swap(myints+3,myvector.begin()+2);  //   myints:  99  20  30 [99]
                                               // myvector:  10  99 [40] 99
    
      cout << "myvector contains:";
      for (vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
        cout << " " << *it;
    
      cout << endl;
    
      return 0;
    }
    
    
    // 307
    #include <algorithm>
    #include <iostream>
    int main(void)
    {
      using namespace std;
      int a = 5;
      int b = 26;
      cout << "交换前 " << "a = " << a << " b = " << b << endl;
      iter_swap(&a, &b);
      cout << "交换后 " << "a = " << a << " b = " << b << endl;
      return 0;
    }


       22.5 区间元素交换swap_ranges
    swap_ranges  Exchange values of two ranges (function template)  

    //   22.5 区间元素交换swap_ranges ---------------------------------------------------
    
    template<class ForwardIterator1, class ForwardIterator2>
      ForwardIterator2 swap_ranges ( ForwardIterator1 first1, ForwardIterator1 last1,
                                     ForwardIterator2 first2 )
    {
      while (first1!=last1) swap(*first1++, *first2++);
      return first2;
    }
    // 两个区间中的元素分别交换。只有三个参数,第四个参数又省了
    // 注意:第二个序列要足够长,否则会出现异常,见下面第二个程序。
    
    
    // swap_ranges example
    #include <iostream>
    #include <algorithm>
    #include <vector>
    using namespace std;
    
    int main () {
      vector<int> first (5,10);        //  first: 10 10 10 10 10
      vector<int> second (5,33);       // second: 33 33 33 33 33
      vector<int>::iterator it;
    
      swap_ranges(first.begin()+1, first.end()-1, second.begin());
    
      // print out results of swap:
      cout << " first contains:";
      for (it=first.begin(); it!=first.end(); ++it)
        cout << " " << *it;
    
      cout << "
    second contains:";
      for (it=second.begin(); it!=second.end(); ++it)
        cout << " " << *it;
    
      cout << endl;
    
      return 0;
    }
    
    
    
    // swap_ranges example. my test:请观察输出结果,当第二个序列不够长时,交换到第一个序列的,不是希望的值!
    #include <iostream>
    #include <algorithm>
    #include <vector>
    using namespace std;
    
    int main () {
      vector<int> first (5,10);        //  first: 10 10 10 10 10
      vector<int> second (2,33);       // second: 33 33
      vector<int>::iterator it;
    
      swap_ranges(first.begin()+1, first.end()-1, second.begin());
    
      // print out results of swap:
      cout << " first contains:";
      for (it=first.begin(); it!=first.end(); ++it)
        cout << " " << *it;
    
      cout << "
    second contains:";
      for (it=second.begin(); it!=second.end(); ++it)
        cout << " " << *it;
    
      cout << endl;
    
      return 0;
    }
    
    
    //308
    #include <algorithm>
    #include <vector>
    #include <iostream>
    using namespace std;
    void print(int x)
    {
      cout << x << " ";
    }
    
    int main(void)
    {
      vector < int > v1, v2;
      v1.push_back(1);
      v1.push_back(3);
      v1.push_back(5);
      v2.push_back(2);
      v2.push_back(4);
      v2.push_back(6);
      //打印v1、v2
      cout << "交换前, v1=";
      for_each(v1.begin(), v1.end(), print);
      cout << "v2=";
      for_each(v2.begin(), v2.end(), print);
      cout << endl;
      //交换v1、v2
      swap_ranges(v1.begin(), v1.end(), v2.begin());
      //打印v1、v2
      cout << "交换后, v1=";
      for_each(v1.begin(), v1.end(), print);
      cout << "v2=";
      for_each(v2.begin(), v2.end(), print);
      cout << endl;
      return 0;
    }


       22.6 元素变换transform
    transform  Apply function to range (function template)  

    //   22.6 元素变换transform ---------------------------------------------------
    
    template < class InputIterator, class OutputIterator, class UnaryOperator >
      OutputIterator transform ( InputIterator first1, InputIterator last1,
                                 OutputIterator result, UnaryOperator op )
    {
      while (first1 != last1)
        *result++ = op(*first1++);  // or: *result++=binary_op(*first1++,*first2++);
      return result;
    }
    // 加转换的copy。而且转换函数可以是一元的,也可以是二元的。
    // 一元的参数有四个:开始两个表示第一序列范围,第三个是结果存放开始位置,第四个是一元函数(对象)
    // 二元的参数有五个:开始两个表示第一序列范围,第三个是第二序列开始位置,第四个是结果存放开始位置,第五个是二元函数(对象)
    
    
    
    // transform algorithm example
    #include <iostream>
    #include <algorithm>
    #include <vector>
    using namespace std;
    
    int op_increase (int i) { return ++i; }
    int op_sum (int i, int j) { return i+j; }
    
    int main () {
      vector<int> first;
      vector<int> second;
      vector<int>::iterator it;
    
      // set some values:
      for (int i=1; i<6; i++) first.push_back (i*10); //  first: 10 20 30 40 50
    
      second.resize(first.size());     // allocate space
      transform (first.begin(), first.end(), second.begin(), op_increase); // 四个参数
                                                      // second: 11 21 31 41 51
    
      transform (first.begin(), first.end(), second.begin(), first.begin(), op_sum); // 五个参数
                                                      //  first: 21 41 61 81 101
    
      cout << "first contains:";
      for (it=first.begin(); it!=first.end(); ++it)
        cout << " " << *it;
    
      cout << endl;
      return 0;
    }
    
    
    // 309
    #include <algorithm>
    #include <vector>
    #include <list>
    #include <iostream>
    using namespace std;
    int square(int x)
    {
      return x *x;
    }
    void print(int x)
    {
      cout << x << "  ";
    }
    int main(void)
    {
      //vector初始化    
      vector < int > v;
      v.push_back(5);
      v.push_back(15);
      v.push_back(25);
      //list初始化
      list < int > l(3);
      //对vector容器元素执行平方运算,放入list容器
      transform(v.begin(), v.end(), l.begin(), square);
      //打印链表元素
      for_each(l.begin(), l.end(), print);
      cout << endl;
      return 0;
    }


       22.7 替换Replace
    replace  Replace value in range (function template)  

    //   22.7 替换 ---------------------------------------------------
    
    
    template < class ForwardIterator, class T >
      void replace ( ForwardIterator first, ForwardIterator last,
                     const T& old_value, const T& new_value )
    {
      for (; first != last; ++first)
        if (*first == old_value) *first=new_value;
    }
    // 在指定范围内旧值换新值
    
    
    
    
    // replace algorithm example
    #include <iostream>
    #include <algorithm>
    #include <vector>
    using namespace std;
    
    int main () {
      int myints[] = { 10, 20, 30, 30, 20, 10, 10, 20 };
      vector<int> myvector (myints, myints+8);            // 10 20 30 30 20 10 10 20
    
      replace (myvector.begin(), myvector.end(), 20, 99); // 10 99 30 30 99 10 10 99
    
      cout << "myvector contains:";
      for (vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
        cout << " " << *it;
    
      cout << endl;
     
      return 0;
    }
    
    
    //311
    #include <algorithm>
    #include <vector>
    #include <iostream>
    using namespace std;
    void print(int x)
    {
      cout << x << "  ";
    }
    int main(void)
    {
      vector < int > v;
      v.push_back(13);
      v.push_back(25);
      v.push_back(27);
      v.push_back(25);
      v.push_back(29);
      //将v的25全部替换为100
      replace(v.begin(), v.end(), 25, 100);
      cout << "v向量元素: ";
      for_each(v.begin(), v.end(), print);
      cout << endl;
      //将iArray的5全部替换为200
      int iArray[7] = {3, 6, 5, 9, 5, 5, 10};
      replace(iArray, iArray + 7, 5, 200);
      cout << "数组iArray元素: ";
      for_each(iArray, iArray + 7, print);
      cout << endl;
      return 0;
    }


       22.8 条件替换replace_if
    replace_if  Replace values in range (function template)  

    //   22.8 条件替换replace_if ---------------------------------------------------
    
    
    template < class ForwardIterator, class Predicate, class T >
      void replace_if ( ForwardIterator first, ForwardIterator last,
                        Predicate pred, const T& new_value )
    {
      for (; first != last; ++first)
        if (pred(*first)) *first=new_value;
    }
    // 当元素满足什么条件时,更新为新值
    
    
    
    // replace_if example
    #include <iostream>
    #include <algorithm>
    #include <vector>
    using namespace std;
    
    bool IsOdd (int i) { return ((i%2)==1); }
    
    int main () {
      vector<int> myvector;
      vector<int>::iterator it;
    
      // set some values:
      for (int i=1; i<10; i++) myvector.push_back(i);          // 1 2 3 4 5 6 7 8 9
      
      replace_if (myvector.begin(), myvector.end(), IsOdd, 0); // 0 2 0 4 0 6 0 8 0
                                                               // 把奇数都替换为0
      cout << "myvector contains:";
      for (it=myvector.begin(); it!=myvector.end(); ++it)
        cout << " " << *it;
    
      cout << endl;
     
      return 0;
    }
    
    
    
    // 312
    
    #include <algorithm>
    #include <vector>
    #include <iostream>
    bool odd(int x)
    {
      return x % 2;
    }
    int main(void)
    {
      using namespace std;
      vector < int > v(10);
      unsigned int i;
      for(i = 0; i < v.size(); i++)
      {
        v[i] = i % 7;
        cout << v[i] << ' ';
      }
      cout << endl;
      //将奇数元素替换为38
      replace_if(v.begin(), v.end(), odd, 38);
      for(i = 0; i < v.size(); i++)
        cout << v[i] << ' ';
      cout << endl;
      return 0;
    }


       22.9 替换和复制replace_copy
    replace_copy  Copy range replacing value (function template)  

    //   22.9 替换和复制replace_copy ---------------------------------------------------
    
    
    template < class InputIterator, class OutputIterator, class T >
      OutputIterator replace_copy ( InputIterator first, InputIterator last,
                                    OutputIterator result, const T& old_value, const T& new_value )
    {
      for (; first != last; ++first, ++result)
        *result = (*first==old_value)? new_value: *first;
      return result;
    }
    // 将第一序列某范围内的元素,复制到第二序列中去。在这个复制过程中,将旧值,替换为新值
    // 前两个参数表示第一序列范围,第三个参数为第二序列开始位置(木有结束位置),第四、五个参数为旧、新值
    
    
    // replace_copy example
    #include <iostream>
    #include <algorithm>
    #include <vector>
    using namespace std;
    
    int main () {
      int myints[] = { 10, 20, 30, 30, 20, 10, 10, 20 };
    
      vector<int> myvector (8);
      replace_copy (myints, myints+8, myvector.begin(), 20, 99);
    
      cout << "myvector contains:";
      for (vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
        cout << " " << *it;
    
      cout << endl;
     
      return 0;
    }
    
    
    // 313
    #include <algorithm>
    #include <list>
    #include <iostream>
    using namespace std;
    void print(int x)
    {
      cout << x << ' ';
    }
    int main(void)
    {
      list < int > l1;
      l1.push_back(1);
      l1.push_back(3);
      l1.push_back(1);
      l1.push_back(6);
      l1.push_back(8);
      //将l1链表元素1替换为100,然后拷贝到l2链表
      list < int > l2(5);
      replace_copy(l1.begin(), l1.end(), l2.begin(), 1, 100);
      cout << "l1保持不变: ";
      for_each(l1.begin(), l1.end(), print);
      cout << "
    l2元素为: ";
      for_each(l2.begin(), l2.end(), print);
      cout << endl;
      return 0;
    }


       22.10 条件替换和复制replace_copy_if
    replace_copy_if  Copy range replacing value (function template)  

    //   22.10 条件替换和复制replace_copy_if ---------------------------------------------------
    
    
    template < class InputIterator, class OutputIterator, class Predicate, class T >
      OutputIterator replace_copy_if ( InputIterator first, InputIterator last,
                                       OutputIterator result, Predicate pred,
                                       const T& new_value )
    {
      for (; first != last; ++first, ++result)
        *result = (pred(*first))? new_value: *first;
      return result;
    }
    // replace_copy的谓词判断版本。 木有旧值了,替换为一个谓词。这样应用范围更广
    
    
    
    // replace_copy_if example
    #include <iostream>
    #include <algorithm>
    #include <vector>
    using namespace std;
    
    bool IsOdd (int i) { return ((i%2)==1); }
    
    int main () {
      vector<int> first,second;
      vector<int>::iterator it;
    
      // set some values:
      for (int i=1; i<10; i++) first.push_back(i);          // 1 2 3 4 5 6 7 8 9
    
      second.resize(first.size());   // allocate space
      replace_copy_if (first.begin(), first.end(), second.begin(), IsOdd, 0);
                                                            // 0 2 0 4 0 6 0 8 0
    
      cout << "second contains:";
      for (it=second.begin(); it!=second.end(); ++it)
        cout << " " << *it;
    
      cout << endl;
     
      return 0;
    }
    
    
    // 314
    #include <algorithm>
    #include <vector>
    #include <list>
    #include <iostream>
    using namespace std;
    
    bool odd(int x)
    {
      return x % 2;
    }
    
    int main(void)
    {
      using namespace std;
      vector < int > v(10);
      unsigned int i;
      for(i = 0; i < v.size(); i++)
      {
        v[i] = i % 7;
        cout << v[i] << ' ';
      }
      cout << endl;
      //将向量v中奇数元素替换为38后,拷贝到链表l
      list < int > l(10);
      replace_copy_if(v.begin(), v.end(), l.begin(), odd, 38);
      list < int > ::iterator list_iter;
      for(list_iter = l.begin(); list_iter != l.end(); list_iter++)
        cout <<  *list_iter << ' ';
      cout << endl;
      return 0;
    }


       22.11 填充fill
    fill  Fill range with value (function template)  

    //   22.11 填充fill ---------------------------------------------------
    
    
    template < class ForwardIterator, class T >
      void fill ( ForwardIterator first, ForwardIterator last, const T& value )
    {
      while (first != last)  *first++ = value;
    }
    // 在序列范围内,均填充上某新值。
    
    
    // fill algorithm example
    #include <iostream>
    #include <algorithm>
    #include <vector>
    using namespace std;
    
    int main () {
      vector<int> myvector (8);                       // myvector: 0 0 0 0 0 0 0 0
    
      fill (myvector.begin(),myvector.begin()+4,5);   // myvector: 5 5 5 5 0 0 0 0
      fill (myvector.begin()+3,myvector.end()-2,8);   // myvector: 5 5 5 8 8 8 0 0
    
      cout << "myvector contains:";
      for (vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
        cout << " " << *it;
    
      cout << endl;
     
      return 0;
    }
    
    
    //315
    #include <algorithm>
    #include <vector>
    #include <iostream>
    using namespace std;
    void print(int x)
    {
      cout << x << ' ';
    }
    int main(void)
    {
      vector < int > v(5);
      fill(v.begin(), v.end(), 30);
      for_each(v.begin(), v.end(), print);
      cout << endl;
      return 0;
    }


       22.12 n次填充fill_n
    fill_n  Fill sequence with value (function template)  

    //   22.12 n次填充fill_n ---------------------------------------------------
    
    
    template < class OutputIterator, class Size, class T >
      void fill_n ( OutputIterator first, Size n, const T& value )
    {
      for (; n>0; --n)  *first++ = value;
    }
    // 在起始位置开始,填充n个value。三个参数:起始位置、几个、新值。
    
    
    
    // fill_n example
    #include <iostream>
    #include <algorithm>
    #include <vector>
    using namespace std;
    
    int main () {
      vector<int> myvector (8,10);        // myvector: 10 10 10 10 10 10 10 10
    
      fill_n (myvector.begin(),4,20);     // myvector: 20 20 20 20 10 10 10 10
      fill_n (myvector.begin()+3,3,33);   // myvector: 20 20 20 33 33 33 10 10
    
      cout << "myvector contains:";
      for (vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
        cout << " " << *it;
    
      cout << endl;
     
      return 0;
    }
    
    
    //315
    #include <algorithm>
    #include <vector>
    #include <iostream>
    using namespace std;
    void print(int x)
    {
      cout << x << ' ';
    }
    int main(void)
    {
      vector < int > v(8);
      fill(v.begin(), v.end(), 1);
      //前5个元素填充为2
      fill_n(v.begin(), 5, 2);
      for_each(v.begin(), v.end(), print);
      cout << endl;
      //全部填充为3
      fill_n(v.begin(), v.size(), 3);
      for_each(v.begin(), v.end(), print);
      cout << endl;
      return 0;
    }


       22.13 随机生成元素generate
    generate  Generate values for range with function (function template)  

    //   22.13 随机生成元素generate ---------------------------------------------------
    
    
    
    template <class ForwardIterator, class Generator>
      void generate ( ForwardIterator first, ForwardIterator last, Generator gen )
    {
      while (first != last)  *first++ = gen();
    }
    // 在指定范围内,填充上第三个参数gen产生的值。
    // gen是个函数(对象),木有参数。所以产生的值,纯粹取决于gen,与容器原来的元素无关
    
    
    
    // generate algorithm example
    #include <iostream>
    #include <algorithm>
    #include <vector>
    #include <ctime>
    #include <cstdlib>
    using namespace std;
    
    // function generator:
    int RandomNumber () { return (rand()%100); } // 产生一百以内的随机数
    
    // class generator:
    struct c_unique {
      int current;
      c_unique() {current=0;} // 构造函数,设成员current初始值为0
      int operator()() {return ++current;} // 调用操作符()重载,使此类能产生函数对象
    } UniqueNumber;
    
    
    int main () {
      srand ( unsigned ( time(NULL) ) );
    
      vector<int> myvector (8);
      vector<int>::iterator it;
    
      generate (myvector.begin(), myvector.end(), RandomNumber);
    
      cout << "myvector contains:";
      for (it=myvector.begin(); it!=myvector.end(); ++it)
        cout << " " << *it;
    
      generate (myvector.begin(), myvector.end(), UniqueNumber);
    
      cout << "
    myvector contains:";
      for (it=myvector.begin(); it!=myvector.end(); ++it)
        cout << " " << *it;
    
      cout << endl;
     
      return 0;
    }
    
    
    //316
    #include <algorithm>
    #include <vector>
    #include <iostream>
    using namespace std;
    //等差数列an+1=an + 3
    class sequence
    {
      public:
        int a;
        sequence(){ a = 0; } // 构造函数
        inline int operator()() // 重载()
        {
            //a = a + 3;
            //return a;
            return a+=3;
        }
    };
    void print(int x)
    {
      cout << x << endl;
    }
    
    int main(void)
    {
      vector < int > v(10);
      sequence an;
      generate(v.begin(), v.end(), an);
      for_each(v.begin(), v.end(), print);
      cout << endl;
      return 0;
    }


       22.14 随机生成n个元素generate_n
    generate_n  Generate values for sequence with function (function template)  

    //   22.14 随机生成n个元素generate_n ---------------------------------------------------
    
    
    template <class OutputIterator, class Size, class Generator>
      void generate ( OutputIterator first, Size n, Generator gen )
    {
      for (; n>0; --n)  *first++ = gen();
    }
    // 在开始位置之后,填充n个gen()产生的值。三个参数:起始位置、几个、gen产生的值。
    
    
    
    // generate_n example
    #include <iostream>
    #include <algorithm>
    #include <vector>
    using namespace std;
    
    int current(0);
    int UniqueNumber () { return ++current; }
    
    int main () {
      vector<int> myvector (9);
      vector<int>::iterator it;
    
      generate_n (myvector.begin(), 6, UniqueNumber); // 因为产生6个,后面几个仍是0
    
      cout << "myvector contains:";
      for (it=myvector.begin(); it!=myvector.end(); ++it)
        cout << " " << *it;
    
      cout << endl;
     
      return 0;
    }
    
    
    //317
    #include <algorithm>
    #include <vector>
    #include <iostream>
    int main(void)
    {
      using namespace std;
      vector < int > v(10);
      //生成3个伪随机数
      generate_n(v.begin(), 3, rand);
      for(unsigned int i = 0; i < v.size(); i++)
        cout << v[i] << ' ';
      cout << endl;
      return 0;
    }


       22.15 移除复制remove_copy
    remove_copy  Copy range removing value (function template)

    //   22.15 移除复制remove_copy ---------------------------------------------------
    
    template <class InputIterator, class OutputIterator, class T>
      OutputIterator remove_copy ( InputIterator first, InputIterator last,
                                   OutputIterator result, const T& value )
    {
      for ( ; first != last; ++first)
        if (!(*first == value)) *result++ = *first;
      return result;
    }
    // 将容器中,不等于某值的元素,复制到另一容器中。
    // 参数有四个:前两个表示范围,第三个是另一容器的起始位置,第四个是某值。
    // 这样就产生了一个问题,另一容器,可以是同一容器么?可以的,请看下面第二个my test程序。
    
    
    
    // remove_copy example
    #include <iostream>
    #include <algorithm>
    #include <vector>
    using namespace std;
    
    int main () {
      int myints[] = {10,20,30,30,20,10,10,20};          // 10 20 30 30 20 10 10 20
      vector<int> myvector (8);  // 如改成2,可以看到,容器不会自动扩充,只会复制进2个数。
      vector<int>::iterator it;
    
      remove_copy (myints,myints+8,myvector.begin(),20); // 10 30 30 10 10 0 0 0
    
      cout << "myvector contains:";
      for (it=myvector.begin(); it!=myvector.end(); ++it)
        cout << " " << *it;
    
      cout << endl;
     
      return 0;
    }
    
    
    
    // remove_copy example。my test: 同一容器是可以的。
    #include <iostream>
    #include <algorithm>
    #include <vector>
    using namespace std;
    
    int main () {
      int myints[] = {10,20,30,30,20,10,10,20};          // 10 20 30 30 20 10 10 20
      vector<int> myvector (myints,myints+8);
      vector<int>::iterator it;
                                                         // 10 30 30 10 10 [10 10 20
      remove_copy (myvector.begin(),myvector.end(),myvector.begin(),20); // 最后三个是原来的
    
    
      cout << "myvector contains:";
      for (it=myvector.begin(); it!=myvector.end(); ++it)
        cout << " " << *it;
    
      cout << endl;
     
      return 0;
    }
    
    
    //318
    #include <algorithm>
    #include <vector>
    #include <iostream>
    using namespace std;
    void print(int x)
    {
      cout << x << "  ";
    }
    int main(void)
    {
      vector < int > v;
      v.push_back(2);
      v.push_back(4);
      v.push_back(3);
      v.push_back(4);
      v.push_back(8);
      //
      int iArray[6]={0};
      //
      remove_copy(v.begin(), v.end(), iArray, 4);
      for_each(v.begin(), v.end(), print);
      cout << endl;
      //
      for_each(iArray, iArray + 6, print);
      cout << endl;
      return 0;
    }


       22.16 条件移除复制remove_copy_if
    remove_copy_if  Copy range removing values (function template)

    //   22.16 条件移除复制remove_copy_if ---------------------------------------------------
    
    
    template <class InputIterator, class OutputIterator, class Predicate>
      OutputIterator remove_copy_if ( InputIterator first, InputIterator last,
                                      OutputIterator result, Predicate pred )
    {
      for ( ; first != last; ++first)
        if (!pred(*first)) *result++ = *first;
      return result;
    }
    // remove_copy 的带谓词判断的版本。把第四个参数,改成了函数(对象)。
    
    
    
    // remove_copy_if example
    #include <iostream>
    #include <algorithm>
    #include <vector>
    using namespace std;
    
    bool IsOdd (int i) { return ((i%2)==1); }
    
    int main () {
      int myints[] = {1,2,3,4,5,6,7,8,9};          
      vector<int> myvector (9);
      vector<int>::iterator it;
    
      remove_copy_if (myints,myints+9,myvector.begin(),IsOdd); // 是奇数的,被删除
    
      cout << "myvector contains:";
      for (it=myvector.begin(); it!=myvector.end(); ++it)
        cout << " " << *it;
    
      cout << endl;
     
      return 0;
    }
    
    
    // 319
    #include <algorithm>
    #include <vector>
    #include <iostream>
    using namespace std;
    void print(int x)
    {
      cout << x << "  ";
    }
    bool even(int x)
    {
      //偶数
      return x % 2 ? 0 : 1;
    }
    int main(void)
    {
      //初始化向量v
      vector < int > v;
      v.push_back(7);
      v.push_back(2);
      v.push_back(5);
      v.push_back(4);
      v.push_back(1);
      //初始化数组iArray
      int iArray[6] = {0};
      //移除v中偶数,剩余元素复制到iArray
      remove_copy_if(v.begin(), v.end(), iArray, even);
      //打印v,v没有改变
      for_each(v.begin(), v.end(), print);
      cout << endl;
      //打印iArray
      for_each(iArray, iArray + 6, print);
      cout << endl;
      return 0;
    }


       22.17 移除remove
    remove  Remove value from range (function template)

    //   22.17 移除remove ---------------------------------------------------
    
    
    template < class ForwardIterator, class T >
      ForwardIterator remove ( ForwardIterator first, ForwardIterator last, const T& value )
    {
      ForwardIterator result = first;
      for ( ; first != last; ++first)
        if (!(*first == value)) *result++ = *first;
      return result;
    }
    // 将容器中等于某值的元素删除。
    // 因为在同一容器中操作,所以返回值值得关注。返回排列完删除某些元素之后的序列的后一个位置。
    // 如果要将数据复制到另一容器,可以考虑用remove_copy
    
    
    // remove algorithm example
    #include <iostream>
    #include <algorithm>
    using namespace std;
    
    int main () {
      int myints[] = {10,20,30,30,20,10,10,20};      // 10 20 30 30 20 10 10 20
    
      // bounds of range:
      int* pbegin = myints;                          // ^
      int* pend = myints+sizeof(myints)/sizeof(int); // ^                       ^
    
      pend = remove (pbegin, pend, 20);              // 10 30 30 10 10 10 10 20
                                                     // ^              ^
      cout << "range contains:";
      for (int* p=pbegin; p!=pend; ++p)
        cout << " " << *p;
    
      cout << endl;
     
      return 0;
    }
    
    
    
    // 321
    #include <algorithm>
    #include <vector>
    #include <iostream>
    using namespace std;
    void print(int x)
    {
      cout << x << "  ";
    }
    int main(void)
    {
      //初始化向量v
      vector < int > v;
      v.push_back(2);
      v.push_back(4);
      v.push_back(3);
      v.push_back(4);
      v.push_back(8);
      //移除4
      vector < int > ::iterator result = remove(v.begin(), v.end(), 4);
      //打印2 3 8
      for_each(v.begin(), result, print);
      cout << endl;
      //打印2 3 8 4 8
      for_each(v.begin(), v.end(), print);
      cout << endl;
      return 0;
    }


       22.18 条件移除remove_if
    remove_if  Remove elements from range (function template)

    //   22.18 条件移除remove_if ---------------------------------------------------
    
    template < class ForwardIterator, class Predicate >
      ForwardIterator remove_if ( ForwardIterator first, ForwardIterator last,
                                  Predicate pred )
    {
      ForwardIterator result = first;
      for ( ; first != last; ++first)
        if (!pred(*first)) *result++ = *first;
      return result;
    }
    // remove的谓词判断版本。 remove的第三个参数,改成了谓词判断。
    
    
    
    // remove_if example
    #include <iostream>
    #include <algorithm>
    using namespace std;
    
    bool IsOdd (int i) { return ((i%2)==1); }
    
    int main () {
      int myints[] = {1,2,3,4,5,6,7,8,9};            // 1 2 3 4 5 6 7 8 9
    
      // bounds of range:
      int* pbegin = myints;                          // ^
      int* pend = myints+sizeof(myints)/sizeof(int); // ^                 ^
    
      pend = remove_if (pbegin, pend, IsOdd);        // 2 4 6 8 5 6 7 8 9
                                                     // ^       ^
      cout << "range contains:";
      for (int* p=pbegin; p!=pend; ++p)
        cout << " " << *p;
    
      cout << endl;
     
      return 0;
    }
    
    
    
    // 322
    #include <algorithm>
    #include <vector>
    #include <iostream>
    using namespace std;
    void print(int x)
    {
      cout << x << "  ";
    }
    bool even(int x)
    {
      //偶数
      return x % 2 ? 0 : 1;
    }
    int main(void)
    {
      //初始化向量v
      vector < int > v;
      v.push_back(7);
      v.push_back(2);
      v.push_back(5);
      v.push_back(4);
      v.push_back(1);
      //移除偶数
      vector < int > ::iterator result = remove_if(v.begin(), v.end(), even);
      //打印7 5 1
      for_each(v.begin(), result, print);
      cout << endl;
      //打印7 5 1 4 1
      for_each(v.begin(), v.end(), print);
      cout << endl;
      return 0;
    }


       22.19 不连续重复元素复制unique_copy
    unique_copy  Copy range removing duplicates (function template)

    //   22.19 不连续重复元素复制unique_copy ---------------------------------------------------
    
    
    template <class InputIterator, class OutputIterator>
      OutputIterator unique_copy ( InputIterator first, InputIterator last,
                                   OutputIterator result )
    {
      *result=*first;
      while (++first != last)
      {
        if (!(*result == *first))  // or: if (!pred(*result,*first)) for the pred version
          *(++result)=*first; // 有点精巧,画图可明白。pred左参数,是左边那个与当前值不同那个元素,右参数是当前元素
      }
      return ++result; // 左闭右开原则,返回时要+1
    }
    // 将连续相等的元素过滤掉,只剩下一个
    // 第四个参数可选,如有,是谓词判断的函数(对象)。这个谓词估计不常用。
    // 第三个参数是输出的第一个位置。可以用同一容器,也可以用不同容器。
    
    // unique_copy example
    #include <iostream>
    #include <algorithm>
    #include <vector>
    using namespace std;
    
    bool myfunction (int i, int j) {
      return (i==j);
    }
    
    int main () {
      int myints[] = {10,20,20,20,30,30,20,20,10};
      vector<int> myvector (9);                            // 0  0  0  0  0  0  0  0  0
      vector<int>::iterator it;
    
      // using default comparison:
      it=unique_copy (myints,myints+9,myvector.begin());   // 10 20 30 20 10 0  0  0  0
                                                           //                ^
    
      sort (myvector.begin(),it);                          // 10 10 20 20 30 0  0  0  0
                                                           //                ^
    
      // using predicate comparison:
      it=unique_copy (myvector.begin(), it, myvector.begin(), myfunction);
                                                           // 10 20 30 20 30 0  0  0  0
                                                           //          ^
    
      myvector.resize( it - myvector.begin() );            // 10 20 30
    
      // print out content:
      cout << "myvector contains:";
      for (it=myvector.begin(); it!=myvector.end(); ++it)
        cout << " " << *it;
    
      cout << endl;
    
      return 0;
    }
    
    
    //323
    #include <algorithm>
    #include <vector>
    #include <iostream>
    using namespace std;
    void print(int x)
    {
      cout << x << ' ';
    }
    int main(void)
    {
      vector < int > v;
      v.push_back(2);
      v.push_back(5);
      v.push_back(5);
      v.push_back(5);
      v.push_back(6);
      v.push_back(5);
      v.push_back(2);
      //
      int iArray[6] = { 0 };
      //
      unique_copy(v.begin(), v.end(), iArray);
      //打印2 5 6 5 2 0
      for_each(iArray, iArray + 6, print);
      cout << endl;
      return 0;
    }


       22.20 剔除连续重复元素unique
    unique  Remove consecutive duplicates in range (function template)

    //   22.20 剔除连续重复元素unique ---------------------------------------------------
    
    
    template <class ForwardIterator>
      ForwardIterator unique ( ForwardIterator first, ForwardIterator last )
    {
      ForwardIterator result=first;
      while (++first != last)
      {
        if (!(*result == *first))  // or: if (!pred(*result,*first)) for the pred version
          *(++result)=*first;
      }
      return ++result;
    }
    // 将连续相等的元素过滤掉,只剩下一个。在同一容器操作。
    // 第三个参数可无,如有是谓词判断函数(对象)
    
    
    
    // unique algorithm example
    #include <iostream>
    #include <algorithm>
    #include <vector>
    using namespace std;
    
    bool myfunction (int i, int j) {
      return (i==j);
    }
    
    int main () {
      int myints[] = {10,20,20,20,30,30,20,20,10};    // 10 20 20 20 30 30 20 20 10
      vector<int> myvector (myints,myints+9);
      vector<int>::iterator it;
    
      // using default comparison:
      it = unique (myvector.begin(), myvector.end()); // 10 20 30 20 10 30 20 20 10
                                                      //                ^
    
      myvector.resize( it - myvector.begin() );       // 10 20 30 20 10
    
      // using predicate comparison:
      unique (myvector.begin(), myvector.end(), myfunction);   // (no changes)
    
      // print out content:
      cout << "myvector contains:";
      for (it=myvector.begin(); it!=myvector.end(); ++it)
        cout << " " << *it;
    
      cout << endl;
    
      return 0;
    }
    
    
    // 325
    #include <algorithm>
    #include <vector>
    #include <iostream>
    using namespace std;
    void print(int x)
    {
      cout << x << ' ';
    }
    int main(void)
    {
      vector < int > v;
      v.push_back(2);
      v.push_back(6);
      v.push_back(6);
      v.push_back(6);
      v.push_back(9);
      v.push_back(6);
      v.push_back(3);
      //
      vector < int > ::iterator result = unique(v.begin(), v.end());
      //打印2 6 9 6 3
      for_each(v.begin(), result, print);
      cout << endl;
      //打印2 6 9 6 3 6 3
      for_each(v.begin(), v.end(), print);
      cout << endl;
      return 0;
    }


       22.21 元素反向reverse
    reverse  Reverse range (function template)

    //   22.21 元素反向reverse ---------------------------------------------------
    
    
    template <class BidirectionalIterator>
      void reverse ( BidirectionalIterator first, BidirectionalIterator last)
    {
      while ((first!=last)&&(first!=--last))
        swap (*first++,*last);
    }
    // 反转元素
    
    
    // reverse algorithm example
    #include <iostream>
    #include <algorithm>
    #include <vector>
    using namespace std;
    
    int main () {
      vector<int> myvector;
      vector<int>::iterator it;
    
      // set some values:
      for (int i=1; i<10; ++i) myvector.push_back(i); // 1 2 3 4 5 6 7 8 9
    
      reverse(myvector.begin(),myvector.end());       // 9 8 7 6 5 4 3 2 1
    
      // print out content:
      cout << "myvector contains:";
      for (it=myvector.begin(); it!=myvector.end(); ++it)
        cout << " " << *it;
    
      cout << endl;
    
      return 0;
    }
    
    
    //326
    #include <algorithm>
    #include <vector>
    #include <iostream>
    using namespace std;
    void print(int x)
    {
      cout << x << ' ';
    }
    
    int main(void)
    {
      vector < int > v(10);
      for(unsigned int i = 0; i < v.size(); i++)
        v[i] = i;
      for_each(v.begin(), v.end(), print);
      cout << endl;
      //
      reverse(v.begin(), v.end());
      for_each(v.begin(), v.end(), print);
      cout << endl;
      return 0;
    }


       22.22 反向复制reverse_copy
    reverse_copy  Copy range reversed (function template)

    //   22.22 反向复制reverse_copy ---------------------------------------------------
    
    
    template <class BidirectionalIterator, class OutputIterator>
      OutputIterator reverse_copy ( BidirectionalIterator first,
                                    BidirectionalIterator last, OutputIterator result )
    {
      while (first!=last) *result++ = *--last;
      return result;
    }
    
    // 从后向前,复制到另一容器。
    // 复制到同一容器,可以吗? 请见以下第二个程序,my test。
    // my test:复制到同一空间(范围),会产生冲突。复制到同一容器不同范围,木有关系。
    
    
    // reverse_copy example
    #include <iostream>
    #include <algorithm>
    #include <vector>
    using namespace std;
    
    int main () {
      int myints[] ={1,2,3,4,5,6,7,8,9};
      vector<int> myvector;
      vector<int>::iterator it;
    
      myvector.resize(9);
    
      reverse_copy (myints, myints+9, myvector.begin());
    
      // print out content:
      cout << "myvector contains:";
      for (it=myvector.begin(); it!=myvector.end(); ++it)
        cout << " " << *it;
    
      cout << endl;
    
      return 0;
    }
    
    
    
    // reverse_copy example。 my test:复制到同一空间(范围),会产生冲突。复制到同一容器不同范围,木有关系。
    #include <iostream>
    #include <algorithm>
    #include <vector>
    using namespace std;
    
    int main () {
      int myints[] ={1,2,3,4,5,6,7,8,9};
      vector<int> v(myints,myints+9);
      vector<int>::iterator it;
      
      reverse_copy(v.begin(),v.begin()+3,v.begin());
    
      // print out content:
      cout << "myvector contains:";
      for (it=v.begin(); it!=v.end(); ++it)
        cout << " " << *it;
    
      cout << endl;
    
      return 0;
    }
    
    
    // 327
    #include <algorithm>
    #include <vector>
    #include <iostream>
    using namespace std;
    void print(int x)
    {
      cout << x << ' ';
    }
    int main(void)
    {
      vector < int > v(10);
      for(unsigned int i = 0; i < v.size(); i++)
        v[i] = i;
      //
      int iArray[10] = {0};
      //
      reverse_copy(v.begin(), v.end(), iArray);
      for_each(iArray, iArray + 10, print);
      cout << endl;
      return 0;
    }


       22.23 旋转rotate
    rotate  Rotate elements in range (function template)

    //   22.23 旋转rotate ---------------------------------------------------
    
    template <class ForwardIterator>
      void rotate ( ForwardIterator first, ForwardIterator middle,
                    ForwardIterator last )
    {
      ForwardIterator next = middle;
      while (first!=next)
      {
        swap (*first++,*next++);
        if (next==last) next=middle;
        else if (first == middle) middle=next; // 这种交换方法,非常新颖。可以画图理解
      }
    }
    // 旋转交换。[first,middle) 与 [middle,last),两区域的元素,进行交换。两区域的元素个数不一定要相等。
    // 如 1,2,3,4,5 ,以4为middle,--> 4,5,1,2,3 。 三个参数:first, middle, last
    
    
    
    // rotate algorithm example
    #include <iostream>
    #include <algorithm>
    #include <vector>
    using namespace std;
    
    int main () {
      vector<int> myvector;
      vector<int>::iterator it;
    
      // set some values:
      for (int i=1; i<10; ++i) myvector.push_back(i); // 1 2 3 4 5 6 7 8 9
    
      rotate(myvector.begin(),myvector.begin()+3,myvector.end());
                                                      // 4 5 6 7 8 9 1 2 3
    
      // print out content:
      cout << "myvector contains:";
      for (it=myvector.begin(); it!=myvector.end(); ++it)
        cout << " " << *it;
    
      cout << endl;
    
      return 0;
    }
    
    
    // 329
    #include <algorithm>
    #include <vector>
    #include <iostream>
    using namespace std;
    void print(int x)
    {
      cout << x << ' ';
    }
    int main(void)
    {
      vector < int > v(10);
      unsigned int i;
      for(i = 0; i < v.size(); i++)
        v[i] = i + 1;
      for_each(v.begin(), v.end(), print);
      cout << endl;
      //
      cout << "开始旋转,middle元素=" << *(v.begin() + 7) << endl;
      rotate(v.begin(), v.begin() + 7, v.end());
      for_each(v.begin(), v.end(), print);
      return 0;
    }


       22.24 旋转复制rotate_copy
    rotate_copy  Copy rotated range (function template)

    //   22.24 旋转复制rotate_copy ---------------------------------------------------
    
    
    template <class ForwardIterator, class OutputIterator>
      OutputIterator rotate_copy ( ForwardIterator first, ForwardIterator middle,
                                   ForwardIterator last, OutputIterator result )
    {
      result=copy (middle,last,result);
      return copy (first,middle,result);
    }
    // 与rotate类似,无非是拷贝到了另一容器。四个参数:first, middle, last, result
    
    
    
    // rotate_copy algorithm example
    #include <iostream>
    #include <algorithm>
    #include <vector>
    using namespace std;
    
    int main () {
      int myints[] = {10,20,30,40,50,60,70};
    
      vector<int> myvector;
      vector<int>::iterator it;
    
      myvector.resize(7);
    
      rotate_copy(myints,myints+3,myints+7,myvector.begin());
    
      // print out content:
      cout << "myvector contains:";
      for (it=myvector.begin(); it!=myvector.end(); ++it)
        cout << " " << *it;
    
      cout << endl;
    
      return 0;
    }
    
    
    
    
    // 329
    #include <algorithm>
    #include <vector>
    #include <iostream>
    using namespace std;
    void print(int x)
    {
      cout << x << ' ';
    }
    int main(void)
    {
      vector < int > v(10);
      for(unsigned int i = 0; i < v.size(); i++)
        v[i] = i + 1;
      //
      int iArray[10] ={0};
      //
      cout << "开始旋转复制,*middle选为" << *(v.begin() + 7) << endl;
      rotate_copy(v.begin(), v.begin() + 7, v.end(), iArray);
      for_each(iArray, iArray + 10, print);
      cout << endl;
      return 0;
    }


       22.25 随机抖动random_shuffle
    random_shuffle  Rearrangle elements in range randomly (function template)  

    //   22.25 随机抖动random_shuffle ---------------------------------------------------
    
    
    template <class RandomAccessIterator, class RandomNumberGenerator>
      void random_shuffle ( RandomAccessIterator first, RandomAccessIterator last,
                            RandomNumberGenerator& rand )
    {
      iterator_traits<RandomAccessIterator>::difference_type i, n;
      n = (last-first);
      for (i=2; i<n; ++i) swap (first[i],first[rand(i)]);
    }
    // shuffle 洗牌。第三个参数可无,如有,是自定义的随机发生器。
    
    
    
    // random_shuffle example
    #include <iostream>
    #include <algorithm>
    #include <functional>
    #include <vector>
    #include <ctime>
    #include <cstdlib>
    using namespace std;
    
    // random generator function:
    ptrdiff_t myrandom (ptrdiff_t i) { return rand()%i;}
    
    // pointer object to it:
    ptrdiff_t (*p_myrandom)(ptrdiff_t) = myrandom;
    
    int main () {
      srand ( unsigned ( time (NULL) ) );
      vector<int> myvector;
      vector<int>::iterator it;
    
      // set some values:
      for (int i=1; i<10; ++i) myvector.push_back(i); // 1 2 3 4 5 6 7 8 9
    
      // using built-in random generator:
      random_shuffle ( myvector.begin(), myvector.end() );
    
      // using myrandom:
      random_shuffle ( myvector.begin(), myvector.end(), p_myrandom);
    
      // print out content:
      cout << "myvector contains:";
      for (it=myvector.begin(); it!=myvector.end(); ++it)
        cout << " " << *it;
    
      cout << endl;
    
      return 0;
    }
    
    
    //331
    #include <algorithm>
    #include <iostream>
    using namespace std;
    void print(int x)
    {
      cout << x << ' ';
    }
    int main(void)
    {
      int iArray[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
      cout << "随机抖动iArray三次" << endl;
      for(int i = 0; i < 3; i++)
      {
        random_shuffle(iArray, iArray + 10);
        for_each(iArray, iArray + 10, print);
        cout << endl;
      }
      return 0;
    }


       22.26 随机采样random_sample
    ...

    //   22.26 随机采样random_sample ---------------------------------------------------
    
    
    
    // 333 。random_sample不是标准函数。我们可以采用另两个函数,来达到相同的目的:
    #include <algorithm>
    #include <vector>
    #include <iostream>
    using namespace std;
    void print(int x)
    {
      cout << x << ' ';
    }
    int main(void)
    {
      vector < int > v(10);
      for(unsigned int i = 0; i < v.size(); i++)
        v[i] = i % 8;
      //
      cout << "v= ";
      for_each(v.begin(), v.end(), print);
      cout << endl;
      //
      const int n = 6; //采样个数
      int iArray[n];
      //random_sample(v.begin(), v.end(), iArray, iArray + n);
      random_shuffle(v.begin(),v.end());
      copy(v.begin(),v.begin()+n,iArray);
      //
      cout << n << "个采样元素为 ";
      for_each(iArray, iArray + n, print);
      cout << endl;
      return 0;
    }


       22.27 容器分割partition
    partition  Partition range in two (function template)  

    //   22.27 容器分割partition ---------------------------------------------------
    
    template <class BidirectionalIterator, class Predicate>
      BidirectionalIterator partition ( BidirectionalIterator first,
                                        BidirectionalIterator last, Predicate pred )
    {
      while (first!=last) // 可以看到快排的影子
      {
        --last;
        while (first!=last && pred(*first)) ++first;
        while (first!=last && !pred(*last)) --last;
        if (first!=last) swap (*first++,*last);
      }
      return first;
    }
    // 把序列分成两部分,前一部分满足条件,后一部分不满足条件。三个参数。
    
    
    
    // partition algorithm example
    #include <iostream>
    #include <algorithm>
    #include <vector>
    using namespace std;
    
    bool IsOdd (int i) { return (i%2)==1; }
    
    int main () {
      vector<int> myvector;
      vector<int>::iterator it, bound;
    
      // set some values:
      for (int i=1; i<10; ++i) myvector.push_back(i); // 1 2 3 4 5 6 7 8 9
    
      bound = partition (myvector.begin(), myvector.end(), IsOdd);
    
      // print out content:
      cout << "odd members:";
      for (it=myvector.begin(); it!=bound; ++it)
        cout << " " << *it;
    
      cout << "
    even members:";
      for (it=bound; it!=myvector.end(); ++it)
        cout << " " << *it;
    
      cout << endl;
    
      return 0;
    }
    
    
    
    // 334
    #include <algorithm>
    #include <iostream>
    using namespace std;
    void print(int x)
    {
      cout << x << ' ';
    }
    bool less10(int x)
    {
      return x < 10;
    }
    int main(void)
    {
      int iArray[10] = { 16,  - 1, 3, 11, 2, 5, 8, 20, 9, 3 };
      int *result = partition(iArray, iArray + 10, less10);
      cout << "按小于10进行分割" << endl;
      for_each(iArray, iArray + 10, print);
      cout << endl;
      //
      cout << "partition算法返回的分界元素为 " <<  *result << endl;
      return 0;
    }


       22.28 容器稳定分割stable_partition
    stable_partition  Divide range in two groups - stable ordering (function template)  

    //   22.28 容器稳定分割stable_partition ---------------------------------------------------
    
    // partition 的稳定版本
    
    // stable_partition example
    #include <iostream>
    #include <algorithm>
    #include <vector>
    using namespace std;
    
    bool IsOdd (int i) { return (i%2)==1; }
    
    int main () {
      vector<int> myvector;
      vector<int>::iterator it, bound;
    
      // set some values:
      for (int i=1; i<10; ++i) myvector.push_back(i); // 1 2 3 4 5 6 7 8 9
    
      bound = stable_partition (myvector.begin(), myvector.end(), IsOdd);
    
      // print out content:
      cout << "odd members:";
      for (it=myvector.begin(); it!=bound; ++it)
        cout << " " << *it;
    
      cout << "
    even members:";
      for (it=bound; it!=myvector.end(); ++it)
        cout << " " << *it;
    
      cout << endl;
    
      return 0;
    }
    
    
    // 338
    #include <algorithm>
    #include <iostream>
    using namespace std;
    void print(int x)
    {
      cout << x << ' ';
    }
    bool less10(int x)
    {
      return x < 10 ? 1 : 0;
    }
    int main(void)
    {
      int iArray[10] = { 16,  - 1, 3, 11, 2, 5, 8, 20, 9, 3 };
      for_each(iArray, iArray + 10, print);
      cout << endl;
      //进行稳定分割
      int *result = stable_partition(iArray, iArray + 10, less10);
      cout << "按小于10稳定分割,iArray数组变为" << endl;
      for_each(iArray, iArray + 10, print);
      cout << endl;
      //
      cout << "iArray数组有如下小于10的整数" << endl;
      for_each(iArray, result, print);
      cout << endl;
      //
      cout << "stable_partition算法返回的分界元素为 " <<  *result << endl;
      return 0;
    }


       22.29 本章小结

     

    /***************************************************************************************************************
    
    //   22.29 本章小结 ---------------------------------------------------
    
       22.1 元素复制copy
    // 把序列一中某范围内的元素,复制到序列二中去。前两个参数是序列一范围,第三个参数是序列二的开始位置
    // 当序列二不够大时,不够装部分将复制不过去。
    
       22.2 反向复制copy_backward
    // 把序列一中某范围内的元素,复制到序列二中去。前两个参数是序列一范围,第三个参数是序列二的结束位置的下一个位置。
    // 两序列可以是同一序列
    
       22.3 元素交换swap
    // 不通过指针交换两个元素
    
       22.4 迭代器交换iter_swap
    // 利用指针(迭代器)交换值
    
       22.5 区间元素交换swap_ranges
    // 两个区间中的元素分别交换。只有三个参数,第四个参数又省了
    // 注意:第二个序列要足够长,否则会出现异常。
    
       22.6 元素变换transform
    // 加转换的copy。而且转换函数可以是一元的,也可以是二元的。
    // 一元的参数有四个:开始两个表示第一序列范围,第三个是结果存放开始位置,第四个是一元函数(对象)
    // 二元的参数有五个:开始两个表示第一序列范围,第三个是第二序列开始位置,第四个是结果存放开始位置,第五个是二元函数(对象)
    
       22.7 替换Replace
    // 在指定范围内旧值换新值
    
       22.8 条件替换replace_if
    // 当元素满足什么条件时,更新为新值
    
       22.9 替换和复制replace_copy
    // 将第一序列某范围内的元素,复制到第二序列中去。在这个复制过程中,将旧值,替换为新值
    // 前两个参数表示第一序列范围,第三个参数为第二序列开始位置(木有结束位置),第四、五个参数为旧、新值
    
       22.10 条件替换和复制replace_copy_if
    // replace_copy的谓词判断版本。 木有旧值了,替换为一个谓词。这样应用范围更广
    
       22.11 填充fill
    // 在序列范围内,均填充上某新值。
    
       22.12 n次填充fill_n
    // 在起始位置开始,填充n个value。三个参数:起始位置、几个、新值。
    
       22.13 随机生成元素generate
    // 在指定范围内,填充上第三个参数gen产生的值。
    // gen是个函数(对象),木有参数。所以产生的值,纯粹取决于gen,与容器原来的元素无关
    
       22.14 随机生成n个元素generate_n
    // 在开始位置之后,填充n个gen()产生的值。三个参数:起始位置、几个、gen产生的值。
    
       22.15 移除复制remove_copy
    // 将容器中,不等于某值的元素,复制到另一容器中。
    // 参数有四个:前两个表示范围,第三个是另一容器的起始位置,第四个是某值。
    // 这样就产生了一个问题,另一容器,可以是同一容器么?可以的。
    
       22.16 条件移除复制remove_copy_if
    // remove_copy 的带谓词判断的版本。把第四个参数,改成了函数(对象)。
    
       22.17 移除remove
    // 将容器中等于某值的元素删除。
    // 因为在同一容器中操作,所以返回值值得关注。返回排列完删除某些元素之后的序列的后一个位置。
    // 如果要将数据复制到另一容器,可以考虑用remove_copy
    
       22.18 条件移除remove_if
    // remove的谓词判断版本。 remove的第三个参数,改成了谓词判断。
    
       22.19 不连续重复元素复制unique_copy
    // 将连续相等的元素过滤掉,只剩下一个
    // 第四个参数可选,如有,是谓词判断的函数(对象)。这个谓词估计不常用。
    // 第三个参数是输出的第一个位置。可以用同一容器,也可以用不同容器。
    
       22.20 剔除连续重复元素unique
    // 将连续相等的元素过滤掉,只剩下一个。在同一容器操作。
    // 第三个参数可无,如有是谓词判断函数(对象)
    
       22.21 元素反向reverse
    // 反转元素
    
       22.22 反向复制reverse_copy
    // 从后向前,复制到另一容器。
    // 复制到同一容器,可以吗? 请见以下第二个程序,my test。
    // my test:复制到同一空间(范围),会产生冲突。复制到同一容器不同范围,木有关系。
    
       22.23 旋转rotate
    // 旋转交换。[first,middle) 与 [middle,last),两区域的元素,进行交换。两区域的元素个数不一定要相等。
    // 如 1,2,3,4,5 ,以4为middle,--> 4,5,1,2,3 。 三个参数:first, middle, last
    
       22.24 旋转复制rotate_copy
    // 与rotate类似,无非是拷贝到了另一容器。四个参数:first, middle, last, result
    
       22.25 随机抖动random_shuffle
    // shuffle 洗牌。第三个参数可无,如有,是自定义的随机发生器。
    
       22.26 随机采样random_sample
    //random_sample不是标准函数。我们可以采用另两个函数,来达到相同的目的
      //random_sample(v.begin(), v.end(), iArray, iArray + n);
      random_shuffle(v.begin(),v.end());
      copy(v.begin(),v.begin()+n,iArray);
    
       22.27 容器分割partition
    // 把序列分成两部分,前一部分满足条件,后一部分不满足条件。三个参数。
    
       22.28 容器稳定分割stable_partition
    // partition 的稳定版本
    
       22.29 本章小结
    // 这就是小结
    
    
    ***********************************************************************************************************/

      

    00TOP00

  • 相关阅读:
    oracle 判断字符串是否包含指定内容
    java 如何使用多线程调用类的静态方法?
    oracle 快速复制表结构、表数据
    oracle 清空表数据的2种方式及速度比较
    一、Instrument之Core Animation工具
    net登录积分(每天登录积分仅仅能加一次) 时间的比較
    正规方程 Normal Equation
    笑谈贝叶斯网络(干货)上
    SQL SERVER 面试题
    好的创始人想要改变世界,最好的创始人还要不放弃——扎克伯格清华中文演讲
  • 原文地址:https://www.cnblogs.com/xin-le/p/4115586.html
Copyright © 2011-2022 走看看