std::copy和std::copy_if是C++ STL中用于序列复制的核心算法,前者无条件复制元素,后者根据谓词条件筛选复制;它们通过迭代器实现容器无关性,适用于数据迁移、过滤、I/O操作等场景,结合插入迭代器(如back_inserter)可安全处理动态容器,使用lambda表达式提升可读性,需注意目标空间不足、迭代器失效、范围重叠及谓词副作用等陷阱,并通过reserve预分配内存优化性能。

C++标准模板库(STL)中的
std::copy
std::copy_if
copy_if
std::copy
std::copy_if
std::vector
std::list
std::string
1. std::copy
std::copy
立即学习“C++免费学习笔记(深入)”;
基本用法:
template<class InputIt, class OutputIt> OutputIt copy(InputIt first, InputIt last, OutputIt d_first);
它接受三个迭代器:
first
last
d_first
copy
[first, last)
d_first
核心思想: 简单的数据迁移。它不关心元素的类型,只要能进行拷贝构造或赋值操作即可。
重要考量:
std::vector
std::back_inserter
std::copy
std::copy_backward
2. std::copy_if
std::copy_if
std::copy
基本用法:
template<class InputIt, class OutputIt, class Predicate> OutputIt copy_if(InputIt first, InputIt last, OutputIt d_first, Predicate pred);
它比
std::copy
pred
bool
true
核心思想: 基于条件的筛选和数据迁移。这使得它在数据清洗、过滤等场景中非常有用。
重要考量:
std::back_inserter
代码示例:
#include <iostream>
#include <vector>
#include <list>
#include <algorithm> // For std::copy and std::copy_if
#include <iterator> // For std::back_inserter, std::ostream_iterator
int main() {
std::vector<int> source_vec = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
std::list<int> destination_list; // 目标容器,动态增长
// 使用 std::copy 将 source_vec 的所有元素复制到 destination_list
// std::back_inserter 会在每次复制时调用 destination_list.push_back()
std::copy(source_vec.begin(), source_vec.end(), std::back_inserter(destination_list));
std::cout << "Copied all elements to list: ";
for (int n : destination_list) {
std::cout << n << " ";
}
std::cout << std::endl; // 输出: 1 2 3 4 5 6 7 8 9 10
// 清空列表,准备进行条件复制
destination_list.clear();
// 使用 std::copy_if 将 source_vec 中的偶数复制到 destination_list
// lambda 表达式作为谓词,判断元素是否为偶数
std::copy_if(source_vec.begin(), source_vec.end(), std::back_inserter(destination_list),
[](int n) { return n % 2 == 0; });
std::cout << "Copied even elements to list: ";
for (int n : destination_list) {
std::cout << n << " ";
}
std::cout << std::endl; // 输出: 2 4 6 8 10
// 另一个例子:直接输出到标准输出
std::cout << "Odd numbers from source_vec: ";
std::copy_if(source_vec.begin(), source_vec.end(),
std::ostream_iterator<int>(std::cout, " "), // 使用 ostream_iterator 直接输出
[](int n) { return n % 2 != 0; });
std::cout << std::endl; // 输出: 1 3 5 7 9
return 0;
}std::copy
std::copy_if
在我看来,
std::copy
std::copy_if
应用场景:
std::vector
std::deque
std::vector
std::copy_if
copy_if
std::istream_iterator
std::ostream_iterator
copy
copy_if
std::transform
std::copy_if
std::copy
std::copy_if
最佳实践:
std::back_inserter
std::front_inserter
std::inserter
std::copy
std::copy_if
std::vector
std::back_inserter
std::list
std::front_inserter
std::back_inserter
std::set
std::map
std::inserter
std::copy_if
std::vector
reserve()
std::copy
memcpy
memmove
std::move
std::move_if
InputIt
OutputIt
std::copy
std::copy_if
在我的编程生涯中,我见过不少开发者在使用
std::copy
std::copy_if
常见的陷阱:
目标容器空间不足(最常见!)
std::vector
std::list
std::deque
std::back_inserter
std::front_inserter
std::inserter
push_back
push_front
insert
int arr[10]; std::copy(vec.begin(), vec.end(), arr);
vec
std::vector<int> src = {1, 2, 3};
std::vector<int> dest(2); // 只有2个元素空间!
// std::copy(src.begin(), src.end(), dest.begin()); // 运行时错误或未定义行为!
std::copy(src.begin(), src.end(), std::back_inserter(dest)); // 正确,dest会自动增长迭代器失效
std::vector
push_back
copy
std::vector
reserve()
copy
源和目标范围重叠问题
std::copy
std::copy
std::copy
std::copy_backward
copy_if
std::copy_if
copy_if
性能问题:
std::vector
std::back_inserter
std::vector
vector
以上就是C++STL算法copy和copy_if使用技巧的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号