zoukankan      html  css  js  c++  java
  • cb46a_c++_STL_算法_逆转和旋转reverse_rotate函数advance

    cb46a_c++_STL_算法_逆转和旋转reverse_rotate
    STL算法--变序性算法
    reverse() 逆转
    reverse_copy()一边复制一般逆转
    rotate()旋转,某个位置开始前后交换位置
    rotate(ivec2.begin(), ivec2.begin() + 2, ivec2.end());
    1,2,3,4,5,6,7,8,9,
    rotate后:
    3,4,5,6,7,8,9,1,2,

    rotate_copy()一边复制一般旋转

    、、、
    next_permutation()
    prev_permutation()
    random_shuffle()
    partition()
    stable_partition()

    //ivec2(ivec);//写法错误
    ivec2 = ivec;//这个可以,容器之间赋值
    ivec2.assign(ivec.begin(), ivec.end());//这个可以,容器之间赋值

    set<int>::iterator pos = iset.begin();
    set迭代器是双向迭代器,不能pos=pos+1;只能用advance(pos,4);前进移动

    其它一般迭代器是随机迭代器,可以使用pos=pos+1的方式。
    #include <iterator>//输出流迭代器 ostream_iterator<int>()

     1 /*cb46a_c++_STL_算法_逆转和旋转reverse_rotate
     2 STL算法--变序性算法
     3 reverse() 逆转
     4 reverse_copy()一边复制一般逆转
     5 rotate()旋转,某个位置开始前后交换位置
     6 rotate(ivec2.begin(), ivec2.begin() + 2, ivec2.end());
     7 1,2,3,4,5,6,7,8,9,
     8 rotate后:
     9 3,4,5,6,7,8,9,1,2,
    10 
    11 rotate_copy()一边复制一般旋转
    12 
    13 、、、
    14 next_permutation()
    15 prev_permutation()
    16 random_shuffle()
    17 partition()
    18 stable_partition()
    19 
    20 //ivec2(ivec);//写法错误
    21     ivec2 = ivec;//这个可以,容器之间赋值
    22     ivec2.assign(ivec.begin(), ivec.end());//这个可以,容器之间赋值
    23 */
    24 
    25 #include <iostream>
    26 #include <algorithm>
    27 #include <vector>
    28 #include <iterator>//用于输出流迭代器
    29 
    30 using namespace std;
    31 
    32 template <typename TT5>
    33 void print1(TT5 &ilist)
    34 {
    35     for (TT5::iterator iter = ilist.begin(); iter != ilist.end(); ++iter)
    36     {
    37         cout << *iter << ' ';
    38         
    39     }
    40     cout << endl;
    41 }
    42 
    43 int main()
    44 {
    45     vector<int> ivec,ivec2;
    46 
    47     for (int i = 1; i <= 9; ++i)
    48         ivec.push_back(i);
    49     print1(ivec);
    50     ivec2.push_back(1);
    51     //ivec2(ivec);//写法错误
    52     ivec2 = ivec;//这个可以,容器之间赋值
    53     ivec2.assign(ivec.begin(), ivec.end());//这个可以,容器之间赋值
    54     cout << "ivec2的值:" << endl;
    55     print1(ivec2);
    56     
    57 
    58     cout << "逆转后的数据:" << endl;
    59     reverse(ivec.begin(),ivec.end());
    60     print1(ivec);
    61 
    62     cout << "一边复制一般逆转,直接传递给cout" << endl;
    63     reverse_copy(ivec.begin(),ivec.end(),ostream_iterator<int>(cout," "));
    64     cout << endl;
    65 
    66     cout << "逆转一部分数据,输出到cout" << endl;
    67     
    68     //print1(ivec);
    69     reverse_copy(ivec.begin()+1,ivec.end()-1,ostream_iterator<int>(cout," "));
    70     cout << endl;
    71     cout << "旋转前:" << endl;
    72     print1(ivec2);
    73     cout << "旋转后:" << endl;
    74     rotate(ivec2.begin(), ivec2.begin() + 2, ivec2.end());
    75     print1(ivec2);
    76 
    77     //也可以这样:
    78     rotate(ivec2.begin(), ivec2.end() - 2, ivec2.end());
    79     print1(ivec2);
    80 
    81     cout << "先用find方法先找到一个数,然后再rotate," << endl;
    82     rotate(ivec2.begin(), find(ivec2.begin(), ivec2.end(), 5), ivec2.end());
    83     print1(ivec2);
    84     
    85 
    86 
    87     return 0;
    88 }


    set<int>::iterator pos = iset.begin();
    set迭代器是双向迭代器,不能pos=pos+1;只能用advance(pos,4);前进移动

    其它一般迭代器是随机迭代器,可以使用pos=pos+1的方式。
    #include <iterator>//输出流迭代器 ostream_iterator<int>()

     1 /*cb46b
     2 
     3 set<int>::iterator pos = iset.begin();
     4 set迭代器是双向迭代器,不能pos=pos+1;只能用advance(pos,4);前进移动
     5 
     6 其它一般迭代器是随机迭代器,可以使用pos=pos+1的方式。
     7 #include <iterator>//输出流迭代器 ostream_iterator<int>()
     8 */
     9 #include <iostream>
    10 #include <algorithm>
    11 #include <set>
    12 #include <iterator>//输出流迭代器
    13 
    14 using namespace std;
    15 
    16 void print3(int elem)
    17 {
    18     cout << elem << ' ';
    19 }
    20 template <typename TT6>
    21 void print2(TT6 iset)
    22 {
    23     /*for (TT6::iterator iter = iset.begin(); iter != iset.end(); ++iter)
    24         cout << *iter << ' ';
    25     cout << endl;*/
    26 
    27     
    28     //两种输出cout的方法,一个是for循环,一个for_each算法。
    29     for_each(iset.begin(), iset.end(), print3);
    30     cout << endl;
    31 }
    32 
    33 int main()
    34 {
    35     set<int> iset;
    36     for (int i = 1; i <= 9; ++i)
    37         iset.insert(iset.end(),i);
    38     print2(iset);
    39 
    40     cout << "使用rotate_copy" << endl;
    41     set<int>::iterator pos = iset.begin();
    42     advance(pos,3);//迭代器前进移动3。类似:pos=pos+3
    43     //set迭代器是双向迭代器,不能pos=pos+1;只能用函数advance(pos,4);前进移动
    44     cout << "从pos位子开始前后交换位置。结果如下:" << endl;
    45     rotate_copy(iset.begin(),pos,iset.end(),ostream_iterator<int>(cout," "));
    46 
    47     cout << "也可以这样做。" << endl;
    48     pos = iset.end();
    49     advance(pos,-2);//倒数第二个位置开始,整体前后交换
    50     rotate_copy(iset.begin(),pos,iset.end(),ostream_iterator<int>(cout," "));
    51 
    52     cout << "使用set的成员函数find来配合使用,位置交换功能" << endl;
    53     rotate_copy(iset.begin(), iset.find(6),iset.end(), ostream_iterator<int>(cout, " "));
    54     return 0;
    55 }
  • 相关阅读:
    Tomcat5配置mysql
    Eclipse完全手册
    MBR是什么
    必杀技公布——用特征码定位关键代码,秒杀MFC程序
    Google C++编程命名约定
    认识硬盘主引导扇区
    c++ const 用法详解
    主引导区
    C++ 关于struce结构体字节对齐
    Fedora 显示设备配置工具介绍
  • 原文地址:https://www.cnblogs.com/txwtech/p/12363803.html
Copyright © 2011-2022 走看看