惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

罗磊的独立博客
SecWiki News
SecWiki News
酷 壳 – CoolShell
酷 壳 – CoolShell
爱范儿
爱范儿
量子位
M
MIT News - Artificial intelligence
GbyAI
GbyAI
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
TaoSecurity Blog
TaoSecurity Blog
博客园 - 【当耐特】
H
Heimdal Security Blog
腾讯CDC
The Last Watchdog
The Last Watchdog
Security Archives - TechRepublic
Security Archives - TechRepublic
Hacker News: Ask HN
Hacker News: Ask HN
S
Schneier on Security
Microsoft Security Blog
Microsoft Security Blog
WordPress大学
WordPress大学
博客园 - 司徒正美
Recent Commits to openclaw:main
Recent Commits to openclaw:main
C
Cybersecurity and Infrastructure Security Agency CISA
S
SegmentFault 最新的问题
大猫的无限游戏
大猫的无限游戏
Application and Cybersecurity Blog
Application and Cybersecurity Blog
F
Full Disclosure
有赞技术团队
有赞技术团队
T
Tailwind CSS Blog
Engineering at Meta
Engineering at Meta
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
T
Threatpost
月光博客
月光博客
A
Arctic Wolf
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
雷峰网
雷峰网
T
Troy Hunt's Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
The Cloudflare Blog
D
DataBreaches.Net
O
OpenAI News
L
LINUX DO - 最新话题
宝玉的分享
宝玉的分享
小众软件
小众软件
V
Vulnerabilities – Threatpost
A
About on SuperTechFans
人人都是产品经理
人人都是产品经理
T
The Exploit Database - CXSecurity.com
Martin Fowler
Martin Fowler
美团技术团队
P
Privacy International News Feed

博客园 - double64

C++ 安全的拷贝赋值(Copy-and-Swap 惯用法) C++ 基类派生类的 static_cast 和 dynamic_cast 转换的简单测试 visual studio 的 snippet 代码片段模板样式 Windows 右键管理官方小程序Autoruns C++ 结合 enum 按位与或组合检测枚举项 std::unique_ptr 当删除器类型为 无状态的时构造可以不用传删除器示例 两个用来写 CLI - Command-Line Interface 的命令解析库 enum class 类型转换 int 微软输入法中如何输出当前时间 音程知识 C++ 模板引用参数的各种情况 英语 12 种时态 Qt 手动添加 Q_OBJECT 需要添加的地方 C# WPF 绑定 ObservableObject 实现 INotifyPropertyChanged 接口 C++ lambda 和 bind 何时优先使用 Windows 下的 Qt 中 min max 函数冲突 C++ RVO 或 NRVO 可能触发的条件 C++ std::unique_ptr 和 std::shared_ptr 都支持自定义删除器(deleter) C++ 智能指针和动态数组 std::vector 插入另一个 vector 的范围元素 Qt tableWidget QTableWidget 常用一些属性设置 C++ 内部类(嵌套类)是可以访问外部类的私有保护成员的 C++ 宏展开顺序 C++ std::round() 四舍五入 cv::Mat 和 HalconCpp::HImage 生成和保存图像简单测试 简单易用的图像库:stb_image Halcon HImage 与 Qt QImage 的相互转换
C++ 标准库 copy_if
double64 · 2025-09-10 · via 博客园 - double64

在 C++ 中,有多种方法可以从一个容器拷贝符合条件的元素到另一个容器。以下是几种常用的方法:

1. 使用 std::copy_if 算法(推荐)

这是最简洁和现代的方法:

#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>

int main() {
    std::vector<int> source = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    std::vector<int> destination;
    
    // 拷贝所有偶数
    std::copy_if(source.begin(), source.end(), 
                 std::back_inserter(destination),
                 [](int x) { return x % 2 == 0; });
    
    // 输出结果
    for (int num : destination) {
        std::cout << num << " ";
    }
    // 输出: 2 4 6 8 10
    
    return 0;
}

2. 使用 std::remove_copy_if(拷贝不满足条件的元素)

#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>

int main() {
    std::vector<int> source = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    std::vector<int> destination;
    
    // 拷贝所有不是偶数的元素(即奇数)
    std::remove_copy_if(source.begin(), source.end(),
                       std::back_inserter(destination),
                       [](int x) { return x % 2 == 0; });
    
    // 输出结果
    for (int num : destination) {
        std::cout << num << " ";
    }
    // 输出: 1 3 5 7 9
    
    return 0;
}

3. 对于关联容器(如 std::set, std::map)

#include <iostream>
#include <set>
#include <vector>
#include <algorithm>
#include <iterator>

int main() {
    std::set<int> source = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    std::vector<int> destination;
    
    // 从 set 拷贝大于 5 的元素到 vector
    std::copy_if(source.begin(), source.end(),
                 std::back_inserter(destination),
                 [](int x) { return x > 5; });
    
    for (int num : destination) {
        std::cout << num << " ";
    }
    // 输出: 6 7 8 9 10
    
    return 0;
}

4. 性能考虑

如果知道大概有多少元素会被拷贝,可以预先分配空间:

#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>

int main() {
    std::vector<int> source = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    
    // 预先分配空间(可选,用于性能优化)
    std::vector<int> destination;
    destination.reserve(source.size()); // 最大可能的大小
    
    std::copy_if(source.begin(), source.end(),
                 std::back_inserter(destination),
                 [](int x) { return x % 2 == 0; });
    
    // 可以收缩空间以节省内存
    destination.shrink_to_fit();
    
    for (int num : destination) {
        std::cout << num << " ";
    }
    
    return 0;
}

总结

  • 推荐使用 std::copy_if:代码简洁,表达意图明确
  • 使用 std::back_inserter 作为输出迭代器,自动处理容器大小
  • 考虑性能:对于大型容器,可以预先分配空间
  • lambda 表达式:提供了灵活的条件判断方式

选择哪种方法取决于具体需求和个人偏好,但 std::copy_if 通常是首选,因为它提供了最好的可读性和简洁性。



note: from deepseek.