stl中copy()函数_std :: copy_if()函数以及C ++ STL中的示例

stl中copy()函数

C ++ STL std :: copy_if()函数 (C++ STL std::copy_if() function)

copy_if() function is a library function of algorithm header, it is used to copy the elements of a container, it copies the certain elements (which satisfy the given condition) of a container from the given start position to another container from the given beginning position.

copy_if()函数算法标头的库函数,用于复制容器的元素,它将容器的某些元素(满足给定条件)从给定的开始位置复制到另一个容器,从给定的开始位置位置。

Note:To use copy_if() function – include <algorithm> header or you can simple use <bits/stdc++.h> header file.

注意:要使用copy_if()函数 –包括<algorithm>头文件,或者您可以简单地使用<bits / stdc ++。h>头文件。

Syntax of std::copy_if() function

std :: copy_if()函数的语法

    std::copy_n(
        iterator source_first, 
        iterator source_end, 
        iterator target_start, 
        UnaryPredicate pred);

Parameter(s):

参数:

  • iterator source_first, iterator source_end – are the iterator positions of the source container.

    迭代器source_first,迭代器source_end –是源容器的迭代器位置。

  • iterator target_start – is the beginning iterator of the target container.

    迭代器target_start –是目标容器的开始迭代器。

  • UnaryPredicate pred – Unary function which accepts an element in the range as an argument, and returns a value convertible to bool.

    UnaryPredicate pred –一元函数,该函数接受范围内的元素作为参数,并返回可转换为bool的值。

Return value: iterator – it is an iterator to the end of the target range where elements have been copied.

返回值: 迭代器 –它是目标元素已复制到目标范围末尾的迭代器。

Example:

例:

    Input:
    //declaring & initializing an int array
    int arr[] = { 10, 20, 30, 40, 50 };
    
    //vector declaration
    vector<int> v1(5);
    
    //copying 5 array elements to the vector
    copy_n(arr, 5, v1.begin());

    Output:
    //if we print the value
    arr: 10 20 30 40 50
    v1: 10 20 30 40 50

C ++ STL程序演示std :: copy_if()函数的使用 (C++ STL program to demonstrate use of std::copy_if() function)

In this example, we are copying only positive elements of the array to the vector.

在此示例中,我们仅将数组的正元素复制到向量。

//C++ STL program to demonstrate use of
//std::copy_if() function
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main()
{
    //declaring & initializing an int array
    int arr[] = { 10, 20, 30, -10, -20, 40, 50 };
    //vector declaration
    vector<int> v1(7);

    //copying 5 array elements to the vector
    copy_if(arr, arr + 7, v1.begin(), [](int i) { return (i >= 0); });

    //printing array
    cout << "arr: ";
    for (int x : arr)
        cout << x << " ";
    cout << endl;

    //printing vector
    cout << "v1: ";
    for (int x : v1)
        cout << x << " ";
    cout << endl;

    return 0;
}

Output

输出量

arr: 10 20 30 -10 -20 40 50
v1: 10 20 30 40 50 0 0

Reference: C++ std::copy_if()

参考: C ++ std :: copy_if()

翻译自: https://www.includehelp.com/stl/std-copy_if-function-with-example.aspx

stl中copy()函数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值