变序列算法属于C++泛型算法中的一种,需要修改容器内容。这些算法包括copy_backward, fill, generate, partition, random_shuffle, remove, replace, rotate, reverse, swap, swap_ranges, transform, unique。这一篇先介绍copy_backward, fill, generate, partition, random_shuffle, remove。
1、copy_backward——从尾向头逆向拷贝
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
int main()
{
//用"..........abcdef.........."初始化source
vector<char> source(10,'.');
for (int c='a'; c<='f'; c++) {
source.push_back(c);
}
source.insert(source.end(),10,'.');
// 打印 source 内容
for (int i = 0; i < source.size(); i++ )
cout << source[i]; cout << endl;
// 拷贝所有的字母元素到 'a' 前面的第三位(从头开始顺向拷贝)
vector<char> c1(source.begin(),source.end());
copy (c1.begin()+10, c1.begin()+16, // 源区间
c1.begin()+7); // 目的地址
copy (c1.begin(),c1.end(),
ostream_iterator<char>(cout,"")); //迭代输出
cout << endl;
// 拷贝所有的字母元素到 'f' 后面的第三位(从尾开始逆向拷贝)
vector<char> c2(source.begin(),source.end());
copy_backward (c2.begin()+10, c2.begin()+16, // 源区间
c2.begin()+19); // 目的地址
copy (c2.begin(),c2.end(),
ostream_iterator<char>(cout,"")); //迭代输出
cout << endl;
}
运行结果:
// ..........abcdef..........
// .......abcdefdef..........
// ..........abcabcdef......asdf
2、fill——向指定区间填充指定内容
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
template <class T>
class Print
{
public:
void operator () (const T& t)
{
cout << t << ' ';
}
};
//-----------------------------------------------
int main ()
{
Print<int> DoPrint;
vector<int> vInt(10);
fill (vInt.begin(), vInt.begin()+5, 1 );
fill (vInt.begin() + 5, vInt.end(), 5 );
for_each (vInt.begin(), vInt.end(), DoPrint);
cout << "\n\n";
return 0;
}
运行结果:
// 1 1 1 1 1 5 5 5 5 5
3、generate、generate_n——填充容器
#include <cstdlib>
#include <list>
#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
list li;
// 插入五个随机数
generate_n (back_inserter(li),
//back_inserter被称作iterator适配器;使元素被插入到作为实参的list的尾部
5, // 个数
rand); // 新的随机数
copy(li.begin(),li.end(),
ostream_iterator<int>(cout," "));
// 覆盖五个新的随机数
generate (li.begin(), li.end(), // 目标区域
rand); // 新的随机数
copy(li.begin(),li.end(),
ostream_iterator<int>(cout," "));
return 0;
}
运行结果:
// 41 18467 6334 26500 19169
// 15724 11478 29358 26962 24464
4、partition——按照指定条件将容器划分为两部分
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
void main()
{
const int VECTOR_SIZE = 8 ;
typedef vector IntVector ;
typedef IntVector::iterator IntVectorIt ;
IntVector Numbers(VECTOR_SIZE); //创建容器
IntVectorIt start, end, it; //创建迭代器
start = Numbers.begin();
end = Numbers.end();
//初始化容器
Numbers[0] = 6
Numbers[1] = 20;
Numbers[2] = 10;
Numbers[3] = 15;
Numbers[4] = 12;
Numbers[5] = 7;
Numbers[6] = 9;
Numbers[7] = 10;
cout << "Before calling partition" << endl ;
// 打印容器内容
cout << "Numbers { " ;
for(it = start; it != end; it++)
cout << *it << " " ;
cout << " }\n" << endl ;
// 给容器分区,小于11的元素全排在大于11的元素前面
it = partition(start, end, bind2nd(less<int>(),11));
cout << "After calling partition" << endl ;
// 打印容器内容
cout << "Numbers { " ;
for(it = start; it != end; it++)
cout << *it << " " ;
cout << " }\n" << endl ;
return 0;
}
运行结果:
// Before calling partition
// Numbers { 6 20 10 15 12 7 9 10 }
//
// After calling partition
// Numbers { 6 10 10 9 7 12 15 20 }
5、random_shuffle——从指定区间中随机取值
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
const int N = 8;
int A[] = {1, 2, 3, 4, 5, 6, 7, 8};
random_shuffle(A, A + N);
copy(A, A + N, ostream_iterator<int>(cout, " "));
}
运行结果:
// 输出结果可能是 7 1 6 3 2 5 4 8,
// 或其他40319种可能(8的阶乘为:40320)
6、remove——从指定区间中删除值为指定值的所有元素,元素数量不变,错位后的剩余元素位保持原值不变
#include <iostream>
#include <list>
#include <algorithm>
using namespace std;
int main()
{
list coll;
for (int i=1; i<=6; ++i) {
coll.push_front(i);
coll.push_back(i);
}
cout << "pre: ";
copy (coll.begin(), coll.end(),
ostream_iterator<int>(cout," "));
cout << endl;
// 删除所有值为3的元素
remove (coll.begin(), coll.end(), 3);
cout << "post: ";
copy (coll.begin(), coll.end(),
ostream_iterator<int>(cout," "));
cout << endl;
return 0;
}
运行结果:
// pre: 6 5 4 3 2 1 1 2 3 4 5 6
// post: 6 5 4 2 1 1 2 4 5 6 5 6
7、remove_copy——从指定区间向另一容器拷贝元素,忽略值为指定值的所有元素
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector V;
V.push_back(-2);
V.push_back(0);
V.push_back(-1);
V.push_back(0);
V.push_back(1);
V.push_back(2);
remove_copy(V.begin(), V.end(),
ostream_iterator<int>(cout, " "),
0);
return 0;
}
运行结果:
// -2 -1 1 2
8、remove_copy_if——从指定区间向另一容器拷贝元素,忽略值不满足指定条件的所有元素
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
int main()
{
vector<int> V1;
V1.push_back(-2);
V1.push_back(0);
V1.push_back(-1);
V1.push_back(0);
V1.push_back(1);
V1.push_back(2);
vector<int> V2;
remove_copy_if(V1.begin(), V1.end(),
back_inserter(V2),
bind2nd(less<int>(), 0));
copy(V2.begin(),V2.end(),
ostream_iterator<int>(cout," "));
cout << endl;
return 0;
}
运行结果:
// 0 0 1 2
9、remove_if——从指定区间中删除值满足指定条件的所有元素
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
int main()
{
vector<int> V;
V.push_back(1);
V.push_back(4);
V.push_back(2);
V.push_back(8);
V.push_back(5);
V.push_back(7);
copy(V.begin(), V.end(),
ostream_iterator<int>(cout, " "));
vector<int>::iterator new_end =
remove_if(V.begin(), V.end(),
compose1(bind2nd(equal_to<int>(), 0),
bind2nd(modulus(), 2)));
V.erase(new_end, V.end());
copy(V.begin(), V.end(),
ostream_iterator<int>(cout, " "));
return 0;
}
运行结果:
// 1 4 2 8 5 7
// 1 5 7
10、replace——将指定区间内的指定值替全部换为另一值
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
void main()
{
const int VECTOR_SIZE = 8 ;
typedef vector<int > IntVector ;
typedef IntVector::iterator IntVectorIt ;
IntVector Numbers(VECTOR_SIZE);
IntVectorIt start, end, it ;
start = Numbers.begin() ;
end = Numbers.end() ;
Numbers[0] = 10 ;
Numbers[1] = 20 ;
Numbers[2] = 10 ;
Numbers[3] = 15 ;
Numbers[4] = 12 ;
Numbers[5] = 7 ;
Numbers[6] = 9 ;
Numbers[7] = 10 ;
cout << "Before calling replace" << endl ;
cout << "Numbers { " ;
for(it = start; it != end; it++)
cout << *it << " " ;
cout << " }\n" << endl ;
// 将所有值为10的元素替换为
replace(start, end, 10, 35) ;
cout << "After calling replace, to replace "
<< "all 10's with 35" << endl ;
cout << "Numbers { " ;
for(it = start; it != end; it++)
cout << *it << " " ;
cout << " }\n" << endl ;
}
运行结果:
// Before calling replace
// Numbers { 10 20 10 15 12 7 9 10 }
// After calling replace, to replace all 10's with 35
// Numbers { 35 20 35 15 12 7 9 35 }
除非注明,文章均为CppLive 编程在线原创,转载请注明出处,谢谢。



