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