STL算法之变序型算法

  1. reverse() 将元素次序逆转
  2. reverse_copy() 将逆序的元素复制到另一序列
  3. rotate() 将[first,mid)[mid,end)元素对调
  4. rotate_copy() 将rotate()后的序列复制到另一序列
  5. next_permutation() 得到元素的下一排列次序(并判断是否是全排列)
  6. prev_permutation() 得到元素的下一排列次序(并判断是否是全排列)
  7. shuffle() 传递随机数引擎将区间元素随机排序
  8. random_shuffle() 将区间元素随机打乱
  9. partition() 将符合某准则的移到前面
  10. stable_partition() 将符合某准则的移到前面并保持稳定性
  11. partition_copy() 将符合准则的复制到序列1,不符合的复制到序列2
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <random>
using namespace std;
// 1. reverse() 将元素次序逆转
// 2. reverse_copy() 将逆序的元素复制到另一序列
// 3. rotate() 将[first,mid)[mid,end)元素对调
// 4. rotate_copy() 将rotate()后的序列复制到另一序列
// 5. next_permutation() 得到元素的下一排列次序(并判断是否是全排列)
// 6. prev_permutation() 得到元素的下一排列次序(并判断是否是全排列)
// 7. shuffle()  传递随机数引擎将区间元素随机排序
// 8. random_shuffle() 将区间元素随机打乱
// 9. partition() 将符合某准则的移到前面
// 10 stable_partition() 将符合某准则的移到前面并保持稳定性
// 11.partition_copy() 将符合准则的复制到序列1,不符合的复制到序列2
void test()
{
    //1
    vector<int> vec{ 1,2,3,4,5,6,7,8,9 };
    reverse(vec.begin(), vec.end());
    for (auto &v : vec)
        cout << v << ends;
    cout << endl;
    //2
    vector<int> a;
    reverse_copy(vec.begin(), vec.end(), back_inserter(a));
    for (auto &v : a)
        cout << v << ends;
    cout << endl;
    //3
    rotate(a.begin(), a.begin() + 4, a.end());
    for (auto &v : a)
        cout << v << ends;
    cout << endl;
    //4
    vector<int> b;
    rotate_copy(a.begin(), a.begin() + 3, a.end(), back_inserter(b));
    for (auto &v : b)
        cout << v << ends;
    cout << endl;
    vector<int> c{ 6,5,4,3,2,1 };
    int count = 0;
    //5
    while (next_permutation(c.begin(), c.end()))
        ++count;
    //6
    //prev_permutation(c.begin(), c.end());
    for (auto &v : c)
        cout << v << ends;
    cout << endl << count << endl;
    //7
    static default_random_engine e;
    shuffle(a.begin(), a.end(),e);
    for (auto &v : a)
        cout << v << ends;
    cout << endl;
    //8
    random_shuffle(a.begin(), a.end());
    for (auto &v : a)
        cout << v << ends;
    cout << endl;
    auto f = [](const int& v)
    {
        return v <= 4;
    };
    //9
    auto partpos = partition(a.begin(), a.end(), f);
    if (partpos != a.end())
    {
        cout << *partpos << endl;
    }
    for (auto &v : a)
        cout << v << ends;
    //10
    auto spos = stable_partition(b.begin(), b.end(), f);
    if (spos != b.end())
    {
        cout << *spos << endl;
    }
    for (auto &v : b)
        cout << v << ends;
    cout << endl;
    vector<int> d;
    vector<int> g;
    //11
    partition_copy(b.begin(), b.end(), back_inserter(d),back_inserter(g), f);
    for (auto &v : d)
        cout << v << ends;
    cout << endl;
    for (auto &v :g)
        cout << v << ends;
    cout << endl;
}
int main()
{
    test(); 
    system("pause");
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值