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

推荐订阅源

C
CXSECURITY Database RSS Feed - CXSecurity.com
Stack Overflow Blog
Stack Overflow Blog
月光博客
月光博客
T
Threat Research - Cisco Blogs
小众软件
小众软件
有赞技术团队
有赞技术团队
酷 壳 – CoolShell
酷 壳 – CoolShell
Apple Machine Learning Research
Apple Machine Learning Research
C
Cyber Attacks, Cyber Crime and Cyber Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
T
Tailwind CSS Blog
Cisco Talos Blog
Cisco Talos Blog
V
V2EX
博客园 - 【当耐特】
C
Cybersecurity and Infrastructure Security Agency CISA
Hugging Face - Blog
Hugging Face - Blog
The Cloudflare Blog
The Last Watchdog
The Last Watchdog
Simon Willison's Weblog
Simon Willison's Weblog
T
Threatpost
S
Secure Thoughts
O
OpenAI News
P
Proofpoint News Feed
S
SegmentFault 最新的问题
Forbes - Security
Forbes - Security
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Application and Cybersecurity Blog
Application and Cybersecurity Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Last Week in AI
Last Week in AI
宝玉的分享
宝玉的分享
Scott Helme
Scott Helme
T
Tenable Blog
A
Arctic Wolf
L
LINUX DO - 热门话题
爱范儿
爱范儿
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
www.infosecurity-magazine.com
www.infosecurity-magazine.com
V
Visual Studio Blog
Hacker News: Ask HN
Hacker News: Ask HN
Hacker News - Newest:
Hacker News - Newest: "LLM"
腾讯CDC
博客园 - Franky
WordPress大学
WordPress大学
Know Your Adversary
Know Your Adversary
博客园_首页
雷峰网
雷峰网
IT之家
IT之家
PCI Perspectives
PCI Perspectives
L
LINUX DO - 最新话题
H
Heimdal Security Blog

博客园 - 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.