Home » C++ STL

std::copy_if() function with example in C++ STL

C++ STL | std::copy_if() function: Here, we are going to learn about the copy_if() function of algorithm header in C++ STL with example.
Submitted by IncludeHelp, on May 20, 2019

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.

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

Syntax of std::copy_if() function

    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.
  • iterator target_start – is the beginning iterator of the target container.
  • UnaryPredicate pred – Unary function which accepts an element in the range as an argument, and returns a value convertible to 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 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()






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.