C++ STL copy_if 用法

一:功能

将容器中满足给定条件的元素拷贝到另一个容器中

二:用法

cpp 复制代码
#include <iostream>
#include <vector>
#include <utility>
#include <algorithm>

int main() {
    std::vector<int> data{ 1, 2, 3, 4, 5, 6, 7, 8, 9};
    std::vector<int> even;

    auto is_even = [](int v) { return v % 2 == 0; };

    std::ranges::copy_if(data, std::back_inserter(even), is_even);
    std::cout << "even == {";
    std::string delim;
    for (auto v : even)
        std::cout << std::exchange(delim, ", ") << v;
    std::cout << "}\n";
}
相关推荐
wengqidaifeng29 分钟前
C++从菜鸟到强手:1.基础入门
开发语言·c++
hhb_61832 分钟前
PHP 8.x 核心特性与工程化开发实践指南
开发语言·php
geovindu1 小时前
go: Flyweight Pattern
开发语言·设计模式·golang·享元模式
xyq20242 小时前
TypeScript中的String类型详解
开发语言
小糖学代码8 小时前
LLM系列:1.python入门:15.JSON 数据处理与操作
开发语言·python·json·aigc
handler018 小时前
从源码到二进制:深度拆解 Linux 下 C 程序的编译与链接全流程
linux·c语言·开发语言·c++·笔记·学习
小白学大数据9 小时前
现代Python爬虫开发范式:基于Asyncio的高可用架构实战
开发语言·爬虫·python·架构
渔舟小调9 小时前
P19 | 前端加密通信层 pikachuNetwork.js 完整实现
开发语言·前端·javascript
不爱吃炸鸡柳9 小时前
数据结构精讲:树 → 二叉树 → 堆 从入门到实战
开发语言·数据结构