TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C++ algorithm partition_copy() function

C++ algorithm partition_copy() function with c++ tutorial for beginners and professionals with examples on adjacent_find(),any_of(), copy(), copy_if(), count(), count_if(), equal(), find(), find_end(), find_first_of(), find_if(), find_if_not(), for_each() etc.

<< Back to CPP

C++ Algorithm partition_copy()

C++ Algorithm partition_copy() function is used to copy the elements for which a condition is true to one destination, and false to another. The elements must belong to a specified range.

Syntax

template <class InputIterator, class OutputIterator1,
          class OutputIterator2, class UnaryPredicate pred>
  pair<OutputIterator1,OutputIterator2>
    partition_copy (InputIterator first, InputIterator last,
                    OutputIterator1 result_true, OutputIterator2 result_false,
                    UnaryPredicate pred);

Parameter

first: An input iterator pointing to the first element in the range to check for a condition.

last: An input iterator pointing to the past last element of the range.

pred: A user-defined unary predicate function that defines the condition to be tested.

result_true: An output iterator used to copy elements for which pred returns true.

result_false: An output iterator used to copy elements for which pred returns false.

Return value

This function returns a pair of iterators with the end of generated sequences addressed by result_true and result_false, respectively.

Complexity

Complexity is linear in the range [first, last) if enough memory is available: Applies pred to each element and performs an assignment for each element.

Data races

The object in the range [first, last) are accessed where the each element is accessed exactly once.

Exceptions

This function throws an exception if any of pred, an element's assignment or an operation on iterator throws an exception.

Note: The invalid parameters cause an undefined behavior.

Example 1

Let's see the simple example to demonstrate the use of partition_copy():

#include <iostream>     // std::cout
#include <algorithm>    // std::partition_copy, std::count_if
#include <vector>       // std::vector

using namespace std;

bool IsOdd (int i) { return (i%2)==1; }

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

  // resize vectors to proper size:
  unsigned n = count_if (foo.begin(), foo.end(), IsOdd);
  odd.resize(n); even.resize(foo.size()-n);

  // partition:
  partition_copy (foo.begin(), foo.end(), odd.begin(), even.begin(), IsOdd);

  // print contents:
  cout << "odd is: ";  for (int& x:odd)  cout << ' ' << x; cout << '\n';
  cout << "even is: "; for (int& x:even) cout << ' ' << x; cout << '\n';

  return 0;
}

Output:

odd is:  1 3 5 7 9
even is:  2 4 6 8

Example 2

Let's see another simple example:

#include <iostream>
#include <algorithm>
#include <utility>

using namespace std;
 
int main()
{
    int arr [10] = {1,2,3,4,5,6,7,8,9,10};
    int true_arr [5] = {0};
    int false_arr [5] = {0};
 
    partition_copy(begin(arr), end(arr), begin(true_arr),begin(false_arr),
                        [] (int i) {return i > 5;});
 
    cout << "true_arr: ";
    for (int x : true_arr) {
        cout << x << ' ';
    }
    cout << '\n'; 
 
    cout << "false_arr: ";
    for (int x : false_arr) {
        cout << x << ' ';
    }
    cout << '\n'; 
 
    return 0;
 
}

Output:

true_arr: 6 7 8 9 10 
false_arr: 1 2 3 4 5

Example 3

Let's see another simple example:

#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <string>

using namespace std;

void print(const string& name, const vector<int>& v)
{
  cout << name << " : ";
  for_each(v.begin(), v.end(), [](int x) {
    cout << x << ",";
  });
  cout << endl;
}

bool is_even(int x) { return x % 2 == 0; }

int main()
{
  vector<int> v = {1, 2, 3, 4, 5};

  vector<int> evens;
  vector<int> odds;
  partition_copy(v.begin(), v.end(),
                      back_inserter(evens),
                      back_inserter(odds), 
                      is_even);

  print("v", v);
  print("evens", evens);
  print("odds ", odds);
  
  return 0;
}

Output:

v : 1,2,3,4,5,
evens : 2,4,
odds  : 1,3,5,

Example 4

Let's see another simple example:

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

bool is_even(int num)
{
    return (num % 2 == 0);
}

void print_vector(const vector<int> & VecRef)
{
    cout << endl;
    cout << "Contents of the vector is : " << endl;
    for (auto it = VecRef.begin(); it != VecRef.end(); ++it) {
        cout << "[" << it - VecRef.begin() << "] : " << *it << endl;
    }
}

int main() 
{
    vector<int> NumbersVec;

    for (int cnt = 1; cnt < 10; ++cnt)
        NumbersVec.push_back(cnt);

    vector<int> EvenVec, OddVec;
    unsigned countEven = count_if(NumbersVec.begin(), NumbersVec.end(), is_even);

    cout << "Evens : " << countEven << endl; //Prints 4
    cout << "Odds : " << NumbersVec.size() - countEven << endl; //Prints 5

    EvenVec.resize(countEven);
    OddVec.resize(NumbersVec.size() - countEven);

    partition_copy(NumbersVec.begin(), NumbersVec.end(), EvenVec.begin(), OddVec.begin(), is_even);

    // partition_copy(NumbersVec.begin(), NumbersVec.end(), back_inserter(EvenVec), back_inserter(OddVec), is_even); This works...

    print_vector(EvenVec);
    print_vector(OddVec); // <== this one crashes

    return 0;
}

Output:

Evens : 4
Odds  : 5

Contents of the vector is : 
[0] : 2
[1] : 4
[2] : 6
[3] : 8

Contents of the vector is : 
[0] : 1
[1] : 3
[2] : 5
[3] : 7
[4] : 9

Next TopicC++ Algorithm




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf