变序列算法属于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,这一篇接着介绍replace, rotate, reverse, swap, swap_ranges, transform, unique。
11、replace_copy——将指定区间内的指定值替全部换为另一值后拷贝入另一容器,原区间值不变
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
void main()
{
const int MAX_ELEMENTS = 8 ;
typedef vector<int > IntVector ;
typedef IntVector::iterator IntVectorIt ;
IntVector Numbers(MAX_ELEMENTS),Result(MAX_ELEMENTS);
IntVectorIt start, end, it, last, resultIt;
Numbers[0] = 10;
Numbers[1] = 20;
Numbers[2] = 10;
Numbers[3] = 15;
Numbers[4] = 12;
Numbers[5] = 7;
Numbers[6] = 9;
Numbers[7] = 10;
start = Numbers.begin() ;
end = Numbers.end() ;
resultIt = Result.begin() ;
cout << "Numbers { " ;
for(it = start; it != end; it++)
cout << *it << " " ;
cout << " }\n" << endl ;
last = replace_copy(start, end, resultIt, 10, 30) ;
cout << "Total number of elements copied to Result = "
<< last - resultIt << endl ;
start = Result.begin() ;
end = Result.end() ;
cout << "Result { " ;
for(it = start; it != end; it++)
cout << *it << " " ;
cout << " }\n" << endl ;
}
运行结果:
// Numbers { 10 20 10 15 12 7 9 10 }
// Total number of elements copied to Result = 8
// Result { 30 20 30 15 12 7 9 30 }
12、replace_copy_if——将指定区间内满足指定条件的值替全部换为另一值后拷贝入另一容器,原区间值不变
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
void main()
{
const int MAX_ELEMENTS = 8 ;
typedef vector<int> IntVector ;
typedef IntVector::iterator IntVectorIt ;
IntVector Numbers(MAX_ELEMENTS),Result(MAX_ELEMENTS);
IntVectorIt start, end, it, last, resultIt;
Numbers[0] = 10 ;
Numbers[1] = 20 ;
Numbers[2] = 10 ;
Numbers[3] = 15 ;
Numbers[4] = 12 ;
Numbers[5] = 7 ;
Numbers[6] = 9 ;
Numbers[7] = 10 ;
start = Numbers.begin() ;
end = Numbers.end() ;
resultIt = Result.begin() ;
cout << "Numbers { " ;
for(it = start; it != end; it++)
cout << *it << " " ;
cout << " }\n" << endl ;
// 将Number中大于等于10的元素替换为30以后再拷贝到result中,result中的值保持不变
last = replace_copy_if(start, end, resultIt,
bind2nd(greater_equal<int>(), 10), 30);
cout << "Total number of elements copied to Result = "
<< last - resultIt << endl ;
start = Result.begin() ;
end = Result.end() ;
cout << "Result { " ;
for(it = start; it != end; it++)
cout << *it << " " ;
cout << " }\n" << endl ;
}
运行结果:
// Numbers { 10 20 10 15 12 7 9 10 }
// Total number of elements copied to Result = 8
// Result { 30 30 30 30 30 7 9 30 }
13、replace_if——将指定区间内满足指定条件的值替全部换为另一值后
#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_if" << endl ;
cout << "Numbers { " ;
for(it = start; it != end; it++)
cout << *it << " " ;
cout << " }\n" << endl ;
// 将所有小于等于10的元素替换为4
replace_if(start, end,
bind2nd(less_equal<int>(), 10), 4 ) ;
cout << "After calling replace_if" << endl ;
cout << "Numbers { " ;
for(it = start; it != end; it++)
cout << *it << " " ;
cout << " }\n" << endl ;
}
运行结果:
// Before calling replace_if
// Numbers { 10 20 10 15 12 7 9 10 }
// After calling replace_if
// Numbers { 4 20 4 15 12 4 4 4 }
14、rotate——容器元素整体移动,使得指定元素成为容器头
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
using namespace std;
int main()
{
vector v(10);
iota(v.begin(),v.end(),1);
copy(v.begin(),v.end(),
ostream_iterator<int>(cout," "));
cout << endl;
// 整体移位使得第二个元素成为第一个
rotate (v.begin(), // 容器头
v.begin() + 1, // 新的容器头
v.end()); // 容器尾
cout << "one left: ";
copy(v.begin(),v.end(),
ostream_iterator<int>(cout," "));
cout << endl;
// 整体移位使得倒数第二个元素成为第一个
rotate (v.begin(), // 容器头
v.end() - 2, // 新的容器头
v.end()); // 容器尾
cout << "two right: ";
copy(v.begin(),v.end(),
ostream_iterator<int>(cout," "));
cout << endl;
// 整体移位使得值为4的元素成为第一个
rotate (v.begin(), // 容器头
find(v.begin(),v.end(),4), // 新的容器头
v.end()); // 容器尾
cout << "4 first: ";
copy(v.begin(),v.end(),
ostream_iterator<int>(cout," "));
cout << endl;
return 0;
}
运行结果:
// 1 2 3 4 5 6 7 8 9 10
// one left: 2 3 4 5 6 7 8 9 10 1
// two right: 10 1 2 3 4 5 6 7 8 9
// 4 first: 4 5 6 7 8 9 10 1 2 3
15、rotate_copy——容器元素整体移动以后存入指定的目标地址,源容器不变
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
using namespace std;
int main()
{
vector<int> v(10);
iota(v.begin(),v.end(),1);
copy(v.begin(),v.end(),
ostream_iterator<int>(cout," "));
cout << endl;
// 左移一位并打印出来
vector<int>::iterator pos = v.begin();
advance(pos,1); //pos += 1;
rotate_copy(v.begin(), // 源容器头
pos, // 新的第一元素
v.end(), // 源容器尾
ostream_iterator<int>(cout," "));
// 目的地址
cout << endl;
// 右移一位并打印出来
pos = v.end();
advance(pos,-2);
rotate_copy(v.begin(), // 源容器头
pos, // 新的第一元素
v.end(), // 源容器尾
ostream_iterator<int>(cout," "));
// 目的地址
cout << endl;
return 0;
}
运行结果:
// 1 2 3 4 5 6 7 8 9 10
// 2 3 4 5 6 7 8 9 10 1
// 9 10 1 2 3 4 5 6 7 8
16、reverse或reverse_copy——容器元素本身顺序反向(或)容器自身顺序不变而反向拷贝到指定的目标地址
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
using namespace std;
int main()
{
vector<int> v(5);
iota(v.begin(),v.end(),1);
copy(v.begin(),v.end(),
ostream_iterator<int>(cout," "));
cout << endl;
// 使元素顺序反向
reverse (v.begin(), v.end());
copy(v.begin(),v.end(),
ostream_iterator<int>(cout," "));
cout << endl;
// 使第二个至倒数第二个元素顺序反向
reverse (v.begin()+1, v.end()-1);
copy(v.begin(),v.end(),
ostream_iterator<int>(cout," "));
cout << endl;
// 将所有元素反向输出
reverse_copy (v.begin(), v.end(), // 源区域
ostream_iterator<int>(cout," "));
// 目标地址
cout << endl;
return 0;
}
运行结果:
// 1 2 3 4 5
// 5 4 3 2 1
// 5 2 3 4 1
// 1 4 3 2 5
17、swap——交换两个元素的值
#include <iostream>
#include <assert>
#include <algorithm>
using namespace std;
int main ()
{
int x = 1;
int y = 2;
cout << "x = " << x << " y = " << y << endl;
assert(x == 1 && y == 2);
swap(x, y);
assert(x == 2 && y == 1);
cout << "x = " << x << " y = " << y << endl;
return 0;
}
运行结果:
// x = 1 y = 2
// x = 2 y = 1
18、swap_ranges——交换两个容器的值
#include <iostream>
#include <algorithm>
#include <vector>
#include <cassert>
int main ()
{
vector<int> V1, V2;
V1.push_back(1);
V1.push_back(2);
V2.push_back(3);
V2.push_back(4);
assert(V1[0] == 1 && V1[1] == 2 &&
V2[0] == 3 && V2[1] == 4);
swap_ranges(V1.begin(), V1.end(), V2.begin());
assert(V1[0] == 3 && V1[1] == 4 &&
V2[0] == 1 && V2[1] == 2);
copy(V1.begin(),V1.end(),
ostream_iterator<int>(cout," "));
cout << endl;
copy(V2.begin(),V2.end(),
ostream_iterator<int>(cout," "));
cout << endl;
return 0;
}
运行结果:
// 3 4
// 1 2
19、transform——将指定容器的元素送入指定函数执行后,取其返回值存入指定地址
#include <iostream>
#include <algorithm>
using namespace std;
double inch_to_cm (double in)
{
return ( in * 2.54 );
}
//----------------------------------------------------
int main ()
{
// 一组以英寸为单位的数值
double inches[] = { 3.5, 6.2, 1.0, 12.87, 4.35 };
double centi[5];
double inch_to_cm (double); //定义函数指针
transform (inches, inches+5, centi, inch_to_cm);
//将inches的元素转化为以厘米为单位的数值后存入centi数组
for (int j=0; j<5; j++)
cout << centi[j] << ' ';
cout << endl;
return 0;
}
运行结果:
// 8.89 15.748 2.54 32.6898 11.049
20、unique——删除指定容器内相邻的重复元素,保证相邻元素值不等
#include <iostream>
#include <algorithm>
#include <vector>
int main ()
{
vector V;
V.push_back(1);
V.push_back(3);
V.push_back(3);
V.push_back(3);
V.push_back(2);
V.push_back(2);
V.push_back(1);
vector<int>::iterator new_end =
unique(V.begin(), V.end());
copy(V.begin(), new_end,
ostream_iterator<int>(cout, " "));
return 0;
}
运行结果:
// 1 3 2 1
21、unique_copy——删除指定容器内相邻的重复元素后再拷贝到目标地址,原容器不变
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
vector<string> v;
// 从标准输入读入单词存入V容器中
copy (istream_iterator<string>(cin),
// 源容器头
istream_iterator<string>(), // 源容器尾
back_inserter(v)); //目的地址
copy (v.begin(),v.end(),
ostream_iterator<string>(cout," "));
// 为元素排序
sort (v.begin(), v.end());
// 无重复地打印元素
unique_copy (v.begin(), v.end(),
ostream_iterator<string>(cout,"\n"));
}
运行结果:
// Denis Anatoliy Anatoliy Wood Wood Lorens
// Anatoliy
// Denis
// Lorens
// Wood
除非注明,文章均为CppLive 编程在线原创,转载请注明出处,谢谢。




话说隔行如隔山嘛,谢谢你的留言。^_^
不多说,支持下了
谢谢支持O(∩_∩)O~