在 C++ 语言 的 标准模板库 ( STL , STL Standard Template Library ) 中 , 提供了 copy 元素复制算法函数 用于 将 一个容器中的元素 复制 到 另外一个 容器中 ;
copy 元素赋值函数 将 输入容器 的 [ 起始迭代器, 终止迭代器 ) 范围 内的 元素 复制 到输出序列中 , 从输出容器 的 指定开始位置迭代器 开始 存放 被复制过来的元素 ;
复制元素操作完成后 , 输出容器中 对应 起始位置迭代器 之后的元素 将被 输入容器 中的元素替换 ;
最终 copy 函数 返回一个迭代器 , 该迭代器 指向 输出容器 中最后一个被复制元素的下一个位置 ;
copy 元素复制算法 函数原型 如下 :
template <class InputIterator, class OutputIterator>
OutputIterator copy(InputIterator first, InputIterator last, OutputIterator result);
代码示例 :
// 输入容器
vector<int> source{9, 5, 2, 7};
// 输出容器 该容器 空间大小不能小于 输入的迭代器范围
vector<int> destination(source.size());
// 遍历打印容器中元素内容
copy(source.begin(), source.end(), destination.begin());
代码示例 :
#include "iostream"
using namespace std;
#include <vector>
#include <algorithm>
#include "functional"
int main() {
// 输入容器
vector<int> source{9, 5, 2, 7};
// 输出容器 该容器 空间大小不能小于 输入的迭代器范围
vector<int> destination(source.size());
// 遍历打印容器中元素内容
copy(source.begin(), source.end(), destination.begin());
// 遍历打印容器中元素内容
for_each(destination.begin(), destination.end(), [](int a) {
cout << a << " ";
});
cout << endl;
// 控制台暂停 , 按任意键继续向后执行
system("pause");
return 0;
};
执行结果 :
9 5 2 7 Press any key to continue . . .
在 C++ 语言 的 标准模板库 ( STL , STL Standard Template Library ) 中 , 提供了 replace 元素替换算法函数 用于 将 一个容器中的 指定迭代器范围 的 元素 中 将 指定的 A 值 替换为 B 值 ;
replace 元素替换函数 将 输入容器 的 [ 起始迭代器, 终止迭代器 ) 范围 内的 元素 指定的 A 值 替换为 B 值 ;
replace 元素替换算法 函数原型 如下 :
template <class ForwardIterator, class T>
void replace(ForwardIterator first, ForwardIterator last, const T& old_value, const T& new_value);
代码示例 :
// 输入容器
vector<int> source{ 9, 5, 2, 7 };
// 将容器中的 2 替换为 3
replace(source.begin(), source.end(), 2, 3);
代码示例 :
#include "iostream"
using namespace std;
#include <vector>
#include <algorithm>
#include "functional"
int main() {
// 输入容器
vector<int> source{ 9, 5, 2, 7 };
// 将容器中的 2 替换为 3
replace(source.begin(), source.end(), 2, 3);
// 遍历打印容器中元素内容
for_each(source.begin(), source.end(), [](int a) {
cout << a << " ";
});
cout << endl;
// 控制台暂停 , 按任意键继续向后执行
system("pause");
return 0;
};
执行结果 :
9 5 3 7 请按任意键继续. . .
在 C++ 语言 的 标准模板库 ( STL , STL Standard Template Library ) 中 , 提供了 replace 元素替换算法函数 用于 将 一个容器中的 指定迭代器范围 的 符合要求的 元素 替换为 新的 值 ;
replace 元素替换函数 将 输入容器 的 [ 起始迭代器, 终止迭代器 ) 范围 内的 元素 中 符合要求的 元素 替换为 新的 值 ;
replace_if 替换符合要求的元素算法 函数原型 如下 :
template <class ForwardIterator, class UnaryPredicate, class T>
void replace_if(ForwardIterator first, ForwardIterator last, UnaryPredicate p, const T& new_value);
代码示例 :
bool is_big_than_3(int num) {
return num > 3;
}
int main() {
// 输入容器
vector<int> source{ 9, 5, 2, 7 };
// 将容器中的 大于 3 的值都替换为 888
replace_if(source.begin(), source.end(), is_big_than_3, 888);
}
代码示例 :
#include "iostream"
using namespace std;
#include <vector>
#include <algorithm>
#include "functional"
bool is_big_than_3(int num) {
return num > 3;
}
int main() {
// 输入容器
vector<int> source{ 9, 5, 2, 7 };
// 将容器中的 大于 3 的值都替换为 888
replace_if(source.begin(), source.end(), is_big_than_3, 888);
// 遍历打印容器中元素内容
for_each(source.begin(), source.end(), [](int a) {
cout << a << " ";
});
cout << endl;
// 控制台暂停 , 按任意键继续向后执行
system("pause");
return 0;
};
执行结果 :
888 888 2 888 请按任意键继续. . .