C++ std::copy_if with std::vector

C++ std::copy_if with std::vector

This C++ article is about the copy_if() algorithm function with std::vector container.
It is an STL algorithm in <algorithm> header file. It copies by checking a condition.
copy_if() has one definition:
template <class InputIterator, class OutputIterator, class UnaryPredicate>
OutputIterator copy_if (InputIterator first, InputIterator last,
OutputIterator result, UnaryPredicate pred);

Return type : returns an integer, i.e the frequency of the element(val) which satisfies the predicate conditions.

Working: The copy_if( ) function takes 4 parameters, two iterators 'first' & 'last' , result , and pred.
Input iterators to the initial and final positions in a sequence. The range copied is [first,last), which contains                       all the elements between first and last, including the element pointed by first but not the element pointed by last.

Output iterator to the initial position of the range where the resulting sequence is stored. The range includes as                           many elements as [first,last)Unary function that accepts an element in the range as argument, and
returns a value convertible to bool. The value returned indicates whether the element is to be copied
(if true, it is copied). The function shall not modify any of its arguments.This can either be a
function pointer or a function object.


#include <iostream>     // std::cout
#include <algorithm>    // std::copy_if, std::distance
#include <vector>       // std::vector
bool predicate(int n) {
   return (n % 5 == 0);
int main () {
  std::vector<int> element = {25,15,5,3,1};
  std::vector<int> element_size (element.size());

  // copy only multiple of five
  auto it = std::copy_if (element.begin(), element.end(), element_size.begin(), predicate);
  element_size.resize(std::distance(element_size.begin(),it));  // shrink container to new size

  std::cout << "element contains:";
  for (int& x: element_size) std::cout << ' ' << x;
  std::cout << '\n';

  return 0;

element contains: 25 15 5

More Articles of Mandeep Sheoran:

Name Views Likes
C++ program to insert an element into binary tree 7886 19
C++ program to find an element into binary tree 869 16
C++ std::is_void 646 15
C++ program to find the closest element in binary search tree 1120 19
C++ program to replace every element with the least greater element on its right 649 12
C++ program to delete an element into binary tree 878 24
C++ program to find maximum element between two nodes of binary search tree 760 20
C++ std::remove_copy_if with std::vectors 642 11
C++ program to print duplicate elements from the binary search tree 3262 15
C++ program to find depth of the deepest odd level node in binary tree 643 23
C++ program to remove duplicate elements from the binary search tree 1913 20
C++ std::rotate_copy with std::vector 626 14
C++ std::copy_n with std::vector 718 22
C++ std::copy_if with std::vector 1739 18
C++ program to print all the elements of binary search tree 8834 22
C++ std::reverse_copy with std::list 673 18
C++ program to print all the elements of binary tree 1611 18
C++ program to print all full nodes in a binary tree 630 25
C++ program to find sink odd nodes in binary tree 649 15
C++ std::is_copy_assignable 678 22
C++ program to check whether a binary tree is a full binary tree or not using recursion 720 19
C++ std::is_copy_constructible 697 27
C++ program to delete an element into binary search tree 3735 18
C++ std::reverse_copy with std::vector 559 18
C++ std::rotate with std::vector 931 15
C++ program to check for symmetric binary tree using recursion 664 25
C++ program to maximum sum from a tree with adjacent levels not allowed 640 15
C++ std::copy_n with std::list 689 21
C++ program to check if two trees are identical using recursion 611 15
C++ std::copy_n 1009 21
C++ std::copy_if with std::list 1237 19
C++ program to print the nodes at odd levels of a tree 631 13
C++ program to find lowest common ancestor in a binary tree 765 29
C++ program to find depth of the deepest odd level leaf node 547 13
C++ std::remove_copy_if with std::list 753 20
C++ program to add all greater values to every node in a given binary search tree 728 15