这篇博客主要介绍的是修正序列算法,修正序列算法的有些操作会改变容器的内容。例如,把一个容器的部分内容复制到同一个容器的另一部分,或者用指定值去填充容器。
下面是STL修正序列算法表
参数 | 说明 |
copy(first,last,first2) | 复制 |
copy_backward(first,last,end2) | 逆向复制 |
fill(first,last,val) | 用val的值填充容器 |
generate(first,last,func) | 以指定的动作运算结果填充容器 |
partition(first,last,pred) | 以一个分割点(比如偶数)来分割容器 |
random_shuffle(first,last) | 随机重排 |
remove(fist,last,val) | 移除某个元素,但不删除 |
replace(fist,last,val,val2) | 用val2取代val |
retate(first,middle,last) | 旋转 |
reverse(first,last) | 颠倒容器中的元素 |
swap(it1,it2) | 交换两个容器的元素 |
transform(first,last,first2,func) | 以两个容器为基础,交互作用产生第三个容器 |
unique(first,last) | 将重复的元素变成唯一的 |
swap_ranges(first,last,first) | 置换指定范围 |
具体实行的代码如下:
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
void print(int val)
{
cout << val << " ";
}
int Sum(int a,int b)
{
return a + b;
}
int random()
{
return rand() % 100 + 1;
}
bool op(int i)
{
return i % 2 == 0;
}
int main()
{
vector<int> ivc1,ivc2;
cout << "请输入ivc1(ivc2)的元素个数:" << endl;
int n,x;
cin >> n;
cout << "请输入ivc1的元素个数:" << endl;
for(int i = 0; i < n; i++)
{
cin >> x;
ivc1.push_back(x);
}
cout << "请输入ivc2的元素个数:" << endl;
for(int i = 0; i < n; i++)
{
cin >> x;
ivc2.push_back(x);
}
cout << "ivc1:" << endl;
for_each(ivc1.begin(),ivc1.end(),print);
cout << endl;
cout << "ivc2:" << endl;
for_each(ivc2.begin(),ivc2.end(),print);
cout << endl << endl;
//using copy()
cout << "After copy():" << endl;
copy(ivc1.begin(),ivc1.end(),ivc2.begin());
cout << "ivc1:" << endl;
for_each(ivc1.begin(),ivc1.end(),print); //将ivc1复制给ivc2
cout << endl;
cout << "ivc2:"<<endl;
for_each(ivc2.begin(),ivc2.end(),print);
cout << endl << endl;
//using reverse
cout << "After reverse():" << endl;
reverse(ivc1.begin(),ivc1.end());
cout << "ivc1:" << endl; //对ivc1进行颠倒
for_each(ivc1.begin(),ivc1.end(),print);
cout << endl << endl;
//using random_shuffle()
cout << "After random_shuffle():" << endl;
random_shuffle(ivc2.begin(),ivc2.end());
cout << "ivc2:" << endl; //对ivc2随机排序
for_each(ivc2.begin(),ivc2.end(),print);
cout << endl << endl;
//using unique()
cout << "After unique():" << endl;
unique(ivc1.begin(),ivc1.end());
cout << "ivc1:" << endl;
for_each(ivc1.begin(),ivc1.end(),print); //变成与末尾相同的元素
cout << endl << endl;
//using swap()
cout << "After swap():" << endl;
swap(ivc1,ivc2); //交换ivc1和ivc2中的元素
cout << "ivc1:" << endl;
for_each(ivc1.begin(),ivc1.end(),print);
cout << endl;
cout << "ivc2:" << endl;
for_each(ivc2.begin(),ivc2.end(),print);
cout << endl << endl;
//using transform
cout << "After transform():" << endl;
vector<int> ivc3;
ivc3.resize(n); //需要提前给ivc3一个容器大小否则transform保存不住数据
transform(ivc1.begin(),ivc1.end(),ivc2.begin(),ivc3.begin(),Sum);
cout << "ivc3:" << endl;
for_each(ivc3.begin(),ivc3.end(),print);
cout << endl << endl;
//using copy_backward()
cout << "After copy_backward():" << endl;
vector<int> ivc4;
ivc4.resize(n);
copy_backward(ivc1.begin(),ivc1.end(),ivc4.end());
cout << "ivc1:" << endl;
for_each(ivc1.begin(),ivc1.end(),print);
cout << endl;
cout << "ivc4:" << endl;
for_each(ivc4.begin(),ivc4.end(),print);
cout << endl << endl;
//using fill()
cout << "After fill():" << endl;
fill(ivc4.begin(),ivc4.end(),8);
cout << "ivc4:" << endl;
for_each(ivc4.begin(),ivc4.end(),print);
cout << endl << endl;
//using generate()
cout << "After generate():" << endl;
generate(ivc4.begin(),ivc4.end(),random); //产生随机数给ivc4
cout << "ivc4:" << endl;
for_each(ivc4.begin(),ivc4.end(),print);
cout << endl << endl;
//using remove()
cout << "After remove():" << endl;
remove(ivc1.begin(),ivc1.end(),3); //移除ivc1中值为3的元素
cout << "ivc1:" << endl;
for_each(ivc1.begin(),ivc1.end(),print);
cout << endl << endl;
//using replace()
cout << "After replace():" << endl;
replace(ivc1.begin(),ivc1.end(),5,77); //将5的值改为77
cout << "ivc1:" << endl;
for_each(ivc1.begin(),ivc1.end(),print);
cout << endl << endl;
//using rotate()
cout << "After rptate():" << endl;
rotate(ivc1.begin(),ivc1.begin()+3,ivc1.end()); //旋转ivc1
cout << "ivc1:" << endl;
for_each(ivc1.begin(),ivc1.end(),print);
cout << endl << endl;
//using partition()
/*
* partition 参数介绍
* 1. begin迭代器
* 2. end 迭代器 注意是前开后闭的 [begin, end)
* 3. pred 是个 callable 类型, 签名如 bool(int v);
* partition的返回值q指向第一个不满足 pred 的位置,
* 最终[begin, q) 符合pred, [q, end) 不符合
* */
cout << "After partition():" << endl;
vector<int>::iterator it;
vector<int>::iterator bound;
it = partition(ivc1.begin(),ivc1.end(),op);
cout << "odd elements:" << endl;
for(bound = ivc1.begin(); bound != it; bound++)
cout << *bound << " ";
cout << endl;
cout << "even elements:" << endl;
for( bound = it; bound != ivc1.end(); bound++)
cout << *bound << " ";
return 0;
}
具体的运行结果如图所示: