在STL中,提供诸多完善的算法,能够对所作用的容器进行修改的算法称为变动性算法。
改变容器中元素值的一般包括两种方法: (1)使用迭代器遍历序列过程中,直接改变元素的值 (2)在元素复制过程中,改变元素的值 变动性算法主要包含8种,分别是: 复制、转换、互换、赋值、替换、逆转、旋转、排序。
1 复制
int dim[] = { 1,3,5,7,9,2,4,6,8 };
vector<int>v1;
v1.assign(dim, dim + 9);
cout << "v1:" << endl;
for_each(v1.begin(), v1.end(), OutToScreen);
cout << endl;
list<int>L1, L2;
copy(v1.begin(), v1.end(),back_inserter(L1));
cout << "L1:" << endl;
for_each(L1.begin(), L1.end(), OutToScreen);
cout << endl;
L2.assign(dim, dim + 9);
cout << "L2:" << endl;
copy(L2.begin(), L2.end(), ostream_iterator<int>(cout, " "));
cout << endl;
2 转换
template<class T>class customFun {
public:
T parm;
customFun(const T& val):parm(val){}
int operator()(T& elem)const {
return(elem * parm);
}
};
template<class T>class customFun2 {
public:
T parm;
customFun2(const T& val) :parm(val) {
};
int operator()(T& elem, T& elem2)const {
return(elem + elem2) * parm;
}
};
int dim[] = { 1,3,5,7,9,2,4,6,8 };
vector<int>v1;
v1.assign(dim, dim + 9);
list<int>L1, L2,L3;
cout << "L1:" << endl;
copy(v1.begin(), v1.end(), back_inserter(L1));
copy(v1.begin(), v1.end(), ostream_iterator<int>(cout, " "));
cout << endl;
cout << "L2=-1*L1:" << endl;
transform(v1.begin(), v1.end(), back_inserter(L2), negate<int>());
copy(L2.begin(), L2.end(), ostream_iterator<int>(cout, " "));
cout << endl;
cout << "L1*L2:改变L2" << endl;
transform(L1.begin(), L1.end(), L2.begin(), L2.begin(), multiplies<int>());
copy(L2.begin(), L2.end(), ostream_iterator<int>(cout, " "));
cout << endl;
cout << "L1=10*L1:" << endl;
transform(L1.begin(), L1.end(), L1.begin(), bind2nd(multiplies<int>(), 10));
copy(L1.begin(), L1.end(), ostream_iterator<int>(cout, " "));
cout << endl;
cout << "L1翻转并取负数:" << endl;
transform(L1.rbegin(), L1.rend(), ostream_iterator<int>(cout, " "), negate<int>());
cout << endl;
cout << "L2/2:" << endl;
transform(L2.begin(), L2.end(), ostream_iterator<int>(cout, " "), bind2nd(divides<int>(), 2));
cout << endl << endl;
cout << "L1:" << endl;
copy(L1.begin(), L1.end(), ostream_iterator<int>(cout, " "));
cout << endl;
cout << "L2:" << endl;
copy(L2.begin(), L2.end(), ostream_iterator<int>(cout, " "));
cout << endl << endl;
cout << "L1+L2" << endl;
transform(L1.begin(), L1.end(), L2.begin(), ostream_iterator<int>(cout, " "), plus<int>());
cout << endl;
cout << "L1-L2" << endl;
transform(L1.begin(), L1.end(), L2.begin(), ostream_iterator<int>(cout, " "), minus<int>());
cout << endl;
cout << "L1*L2" << endl;
transform(L1.begin(), L1.end(), L2.begin(), ostream_iterator<int>(cout, " "), multiplies<int>());
cout << endl;
cout << "L1/L2" << endl;
transform(L1.begin(), L1.end(), L2.begin(), ostream_iterator<int>(cout, " "), divides<int>());
cout << endl;
cout << "L3=L1+2" << endl;
transform(L1.begin(), L1.end(), back_inserter(L3), bind2nd(plus<int>(), 2));
copy(L3.begin(), L3.end(), ostream_iterator<int>(cout, " "));
cout << endl;
cout << "自定义一个参数的仿函数例子 L1*4" << endl;
transform(L1.begin(), L1.end(), ostream_iterator<int>(cout, " "), customFun<int>(4));
cout << endl;
cout << "自定义两个参数的仿函数例子实现相加再相乘 (L1+L2)*3" << endl;
transform(L1.begin(), L1.end(), L2.begin(), ostream_iterator<int>(cout, " "), customFun2<int>(3));
cout << endl;
3 互换
int dim1[] = { 1,2,3,4,5,6,7,8,9 };
int dim2[] = { 11,12,13,14,15,16,17,18,19 };
vector<int>v1;
vector<int>v2;
v1.assign(dim1, dim1 + 9);
copy(dim2, dim2 + 9, back_inserter(v2));
cout << "v1:" << endl;
copy(v1.begin(), v1.end(), ostream_iterator<int>(cout, " "));
cout << endl;
cout << "v2:" << endl;
copy(v2.begin(), v2.end(), ostream_iterator<int>(cout, " "));
cout << endl;
swap(v1, v2);
cout << "v1与v2全部交换后" << endl;
cout << "v1:" << endl;
copy(v1.begin(), v1.end(), ostream_iterator<int>(cout, " "));
cout << endl;
cout << "v2:" << endl;
copy(v2.begin(), v2.end(), ostream_iterator<int>(cout, " "));
cout << endl;
swap_ranges(v1.begin(), v1.begin() + 5, v2.begin());
cout << "v1与v2前面五个元素进行交换后" << endl;
cout << "v1:" << endl;
copy(v1.begin(), v1.end(), ostream_iterator<int>(cout, " "));
cout << endl;
cout << "v2:" << endl;
copy(v2.begin(), v2.end(), ostream_iterator<int>(cout, " "));
cout << endl;
4 赋值
int Fib(void) {
static int sum = 0;
static int r = 0;
sum += r;
r++;
return(sum);
}
vector<int>v1;
vector<int>v2;
vector<int>v3;
int dim1[] = { 1,2,3,4,5,6,7,8,9 };
v1.assign(dim1, dim1 + 9);
copy(v1.begin(), v1.end(), back_inserter(v2));
v3 = v1;
cout << "v1" << endl;
copy(v1.begin(), v1.end(), ostream_iterator<int>(cout, " "));
cout << endl;
fill(v1.begin(), v1.begin() + 5, 0);
cout << "将v1前面五个数赋值为0" << endl;
copy(v1.begin(), v1.end(), ostream_iterator<int>(cout, " "));
cout << endl;
fill_n(v1.begin(), 4, 10);
cout << "将v1前面4个数赋值为10" << endl;
copy(v1.begin(), v1.end(), ostream_iterator<int>(cout, " "));
cout << endl;
generate(v2.begin(), v2.end(), rand);
cout << "随机给v2赋值" << endl;
copy(v2.begin(), v2.end(), ostream_iterator<int>(cout, " "));
cout << endl;
generate_n(v3.begin(), 3, rand);
cout << "随机给v3前三个元素赋值" << endl;
copy(v3.begin(), v3.end(), ostream_iterator<int>(cout, " "));
cout << endl;
int Fib(void);
generate_n(v3.rbegin(), 5, Fib);
cout << "随机给v3最后5个元素赋值" << endl;
copy(v3.begin(), v3.end(), ostream_iterator<int>(cout, " "));
cout << endl;
5 替换
vector<int>v1;
int dim1[] = { 1,7,3,7,5,6,7,8,9 };
v1.assign(dim1, dim1 + 9);
cout << "v1" << endl;
copy(v1.begin(), v1.end(), ostream_iterator<int>(cout, " "));
cout << endl;
cout << "将v1中等于7的值全部替换为0" << endl;
replace(v1.begin(), v1.end(), 7, 0);
copy(v1.begin(), v1.end(), ostream_iterator<int>(cout, " "));
cout << endl;
cout << "将v1中小于5的值全部替换为10" << endl;
replace_if(v1.begin(), v1.end(), bind2nd(less<int>(), 5), 10);
copy(v1.begin(), v1.end(), ostream_iterator<int>(cout, " "));
cout << endl;
6 逆转
vector<int>v1,v2;
int dim1[] = { 1,2,3,4,5,6,7,8,9 };
list<int>L1;
v1.assign(dim1, dim1 + 9);
cout << "v1" << endl;
copy(v1.begin(), v1.end(), ostream_iterator<int>(cout, " "));
cout << endl;
L1.assign(v1.begin(), v1.end());
cout << "L1" << endl;
copy(L1.begin(), L1.end(), ostream_iterator<int>(cout, " "));
cout << endl;
cout << "逆转v1中间部分" << endl;
reverse(v1.begin() + 2, v1.end() - 2);
copy(v1.begin(), v1.end(), ostream_iterator<int>(cout, " "));
cout << endl;
cout << "逆转L1中间部分" << endl;
list<int>::iterator posf = L1.begin();
list<int>::iterator pose = L1.end();
advance(posf, 2);
advance(pose, -2);
reverse(posf, pose);
copy(L1.begin(), L1.end(), ostream_iterator<int>(cout, " "));
cout << endl;
cout << "逆转V1赋值给V2" << endl;
reverse_copy(v1.begin(), v1.end(), back_inserter(v2));
copy(v2.begin(), v2.end(), ostream_iterator<int>(cout, " "));
cout << endl;
cout << "逆转V2" << endl;
reverse_copy(v2.begin()+2, v2.end()-2, ostream_iterator<int>(cout," "));
cout << endl;
7 旋转
vector<int>v1,v2;
int dim1[] = { 1,2,3,4,5,6,7,8,9 };
v1.assign(dim1, dim1 + 9);
cout << "v1" << endl;
copy(v1.begin(), v1.end(), ostream_iterator<int>(cout, " "));
cout << endl;
cout << "v1第一次旋转" << endl;
rotate(v1.begin(), v1.begin() + 3, v1.end()-2);
copy(v1.begin(), v1.end(), ostream_iterator<int>(cout, " "));
cout << endl;
cout << "v1第二次旋转" << endl;
rotate(v1.begin()+3, v1.begin() + 5, v1.end());
copy(v1.begin(), v1.end(), ostream_iterator<int>(cout, " "));
cout << endl;
cout << "v1第三次旋转,赋值给v2" << endl;
rotate_copy(v1.begin(), v1.begin() + 2, v1.end(), back_inserter(v2));
copy(v2.begin(), v2.end(), ostream_iterator<int>(cout, " "));
cout << endl;
cout << "v2第一次旋转" << endl;
rotate_copy(v2.begin()+2, v2.begin() + 4, v2.end()-2, ostream_iterator<int>(cout," "));
cout << endl;
8 排序
int dim1[] = { 1,5,9,4,6,8,7,3};
vector<int>v1;
v1.assign(dim1, dim1 + 8);
cout << "v1" << endl;
copy(v1.begin(), v1.end(), ostream_iterator<int>(cout, " "));
cout << endl;
cout << "打乱v1中的次序" << endl;
random_shuffle(v1.begin(), v1.end());
copy(v1.begin(), v1.end(), ostream_iterator<int>(cout, " "));
cout << endl;
cout << "从新排序v1" << endl;
sort(v1.begin(), v1.end());
copy(v1.begin(), v1.end(), ostream_iterator<int>(cout, " "));
cout << endl;
vector<int>v2;
v2.push_back(3);
v2.push_back(2);
v2.push_back(7);
v2.push_back(9);
v2.push_back(6);
v2.push_back(1);
v2.push_back(4);
v2.push_back(5);
cout << "v2" << endl;
copy(v2.begin(), v2.end(), ostream_iterator<int>(cout, " "));
cout << endl;
vector<int>::iterator pos1, pos2;
cout << "v1不排序" << endl;
pos1 = partition(v1.begin(), v1.end(), not1(bind2nd(modulus<int>(), 2)));
copy(v1.begin(), v1.end(), ostream_iterator<int>(cout, " "));
cout << endl;
cout << "v2相对排序" << endl;
pos1 = stable_partition(v2.begin(), v2.end(), not1(bind2nd(modulus<int>(), 2)));
copy(v2.begin(), v2.end(), ostream_iterator<int>(cout, " "));
cout << endl;
|