C++ boost::algorithm::partition_copy()














































C++ boost::algorithm::partition_copy()



Description:
The header 'boost/algorithm/cxx11/partition_copy.hpp'   contains the algorithm partition_copy() in C++ boost library. This function copies the elements from the range [first, last) to two different sequence depending on the value returned by the predicate. If the predicate returns 'True' the element is copied to the range beginning at out_true and rest of the elements are copied to the out_false.
 
Syntax:
template<typename InputIterator, typename OutputIterator1, typename OutputIterator2, typename UnaryPredicate>
     
BOOST_CXX14_CONSTEXPR std::pair< OutputIterator1, OutputIterator2 >
     
partition_copy(InputIterator, InputIterator, OutputIterator1, OutputIterator2, UnaryPredicate);

template<typename Range, typename OutputIterator1, typename OutputIterator2, typename UnaryPredicate>
     
BOOST_CXX14_CONSTEXPR std::pair< OutputIterator1, OutputIterator2 >
     
partition_copy(const Range &, OutputIterator1, OutputIterator2, UnaryPredicate);

Parameters:
The following parameters are accepted by the function:
1.) first: It specifies the initial position of the sequence by input iterator (first is inclusive in range).
2.) last: It specifies the end position of the sequence by input iterator (last is exclusive in range).
3.) out_true: It specifies the beginning of the output range for the elements that satisfy the predicate
4.) out_false: It specifies the beginning of the output range for the elements that don't satisfy the predicate

3.) UnaryPredicate: It is a unary function that accepts the elements of the sequence in the range provided in argument and returns true or false based on some condition.

The function returns a std::pair constructed from the iterator to the end of the out_true range and the iterator to the end of the out_false range.


Program:
#include <boost/algorithm/cxx11/partition_copy.hpp>
#include <vector>
#include <iostream>
using namespace std;

int main()
{
   
vector<int> Arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, Arr_True, Arr_False;
   
/* Predicate to check even elements in sequence */
   
auto predicate = [](int element) { return element % 2 == 0; };
   
/* Predicate which determine if the element will be stored in Arr_True or Arr_False
    ** back_inserter generates iterator for inserting element at the end of array
    */

    boost::algorithm::partition_copy(Arr.begin(), Arr.end(), back_inserter(Arr_True), back_inserter(Arr_False), predicate);
   
cout << "Even elements are: ";
   
for(auto i : Arr_True)
       
cout << i << " ";
   
cout << endl << "Odd elements are: ";
   
for(auto i : Arr_False)
       
cout << i << " ";
}


Example:
Output: Even elements are: 2 4 6 8 10
Odd elements are: 1 3 5 7 9


Complexity:
Time Complexity: The time complexity is linear between first and last element of the sequence. Predicate is called for the same number of time.

Please write on comment section if you find any mistake or for any suggestions.

More Articles of Abhishek Kumar Singh:

Name Views Likes
C++ boost::range::replace_if 1020 0
C++ boost::range::copy_backward 759 1
C++ boost::range::max_element 1234 0
C++ boost::range::inplace_merge 686 0
C++ boost::range::copy 1351 1
C++ boost::algorithm::is_partitioned() 857 1
C++ boost::algorithm::copy_if() 1048 0
C++ boost::range::for_each (version 2) 768 0
C++ boost::range::set_symmetric_difference 684 0
C++ boost::remove_copy_if 769 0
C++ boost::range::set_intersection 1028 0
C++ boost::range::find_end 707 0
C++ boost::range::remove_erase_if 1652 0
C++ boost::range::push_back 1174 0
C++ boost::range::generate 672 0
C++ boost::algorithm::any_of() 739 0
C++ boost::range::insert 737 0
C++ boost::range::remove_erase 948 0
C++ boost::range::reverse 1266 0
C++ boost::algorithm::equal() 775 1
C++ boost::range::copy_n 848 0
C++ boost::range::random_shuffle 1286 1
C++ boost::algorithm::partition_point() 653 0
C++ boost::algorithm::one_of_equal() 545 0
C++ boost::algorithm::all_of() 918 1
C++ boost::range::merge 1059 0
C++ boost::range::reverse_copy 773 0
C++ boost::range::find 781 0
C++ boost::range::fill_n 627 0
Removing duplicate elements from std::vector (using std::unique and std::set) 2476 1
C++ boost::range::equal 739 0
C++ boost::algorithm::iota() 1015 0
C++ boost::range::is_sorted 852 0
test article 800 2
C++ boost::algorithm::is_permutation() 883 1
C++ boost::partial_sum 893 0
C++ boost::range::partial_sort 948 1
C++ boost::range::min_element 1085 0
C++ boost::range::iota 1084 0
C++ boost::range::set_union 736 0
C++ boost::algorithm::partition_copy() 1093 3
C++ boost::range::swap_ranges 634 0
C++ boost::range::for_each 920 0
C++ boost::range::upper_bound 1091 0
C++ boost::range::binary_search 1249 0
C++ boost::algorithm::all_of_equal() 569 0
C++ boost::algorithm::copy_n() 707 1
C++ boost::range::lower_bound 1048 0
C++ boost::algorithm::gather() 1534 0
C++ boost::algorithm::none_of_equal() 612 0
C++ boost::algorithm::one_of() 726 2
C++ boost::range::rotate 877 0
C++ boost::algorithm::any_of_equal() 881 0
Use of Comparator in C++ 2130 0
C++ boost::range::count 828 0
C++ boost::range::replace_copy_if 649 0
C++ boost::range::remove 841 2
C++ boost::remove_if 1463 1
C++ boost::range::nth_element 1155 1
C++ boost::range::partition 759 1
C++ boost::range::erase 672 0
C++ boost::range::fill 908 1
C++ boost::range::find_if 1364 0
C++ boost::range::lexicographical_compare 817 0
C++ boost::algorithm::none_of() 600 0
C++ boost::algorithm::hex() 3753 0
C++ boost::range::replace 751 0
C++ boost::range::replace_copy 825 1
C++ boost::range::set_difference 980 0
C++ boost::range::overwrite 729 0
C++ boost::range::count_if 982 0
C++ boost::range::push_front 674 0
C++ boost::range::includes 670 0
C++ boost::algorithm::is_sorted() 713 0
C++ boost::range::remove_copy 633 1
C++ boost::algorithm::minmax_element 632 1
Deletion of leaf node of a Binary Search Tree 1728 1
Nth Fibonacci Number (Recursive Solution, Dynamic Programming, Iterative Solution 3928 1
C++ boost::range::rotate_copy 664 0

Comments