TheDeveloperBlog.com

Home | Contact Us

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

C++ algorithm partition_point() function

C++ algorithm partition_point() 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_point()

C++ Algorithm partition_point() function is used to return the first element in the given range for which pred is not true. The elements are sorted in a way that the elements which satisfy the condition come before those who do not satisfy.

Syntax

template <class ForwardIterator, class UnaryPredicate>
ForwardIterator partition_point (ForwardIterator first, ForwardIterator last,
                                   UnaryPredicate pred);

Parameter

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

last: A forward iterator pointing to the past last element of the range.

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

Return value

This function returns a forward iterator to point to the first element that does not fulfill the condition tested by pred or returns last if one is not found.

Complexity

Complexity is logarithmic in the range [first, last).

Data races

Some of the object in the range [first, last) are accessed.

Exceptions

This function throws an exception if either an element's comparison 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_point():

#include <algorithm>
#include <array>
#include <iostream>
#include <iterator>

using namespace std;
 
int main()
{
    array<int, 9> v = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
 
    auto is_even = [](int i)
        { return i % 2 == 0; };
    partition(v.begin(), v.end(), is_even);
 
    auto p = std::partition_point(v.begin(), v.end(), is_even);
 
    cout << "Before partition:\n    ";
    copy(v.begin(), p, ostream_iterator<int>(cout, " "));
    cout << "\nAfter partition:\n    ";
    copy(p, v.end(), ostream_iterator<int>(cout, " "));
    
    return 0;
}

Output:

Before partition:
    8 2 6 4 
After partition:
    5 3 7 1 9

Example 2

Let's see another simple example:

#include <iostream>     // std::cout
#include <algorithm>    // std::partition, std::partition_point
#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;

  partition (foo.begin(),foo.end(),IsOdd);

  auto it = partition_point(foo.begin(),foo.end(),IsOdd);
  odd.assign (foo.begin(),it);

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

  return 0;
}

Output:

odd: 1 9 3 7 5

Example 3

Let's see another simple example:

#include<iostream> 
#include<algorithm> // for partition algorithm 
#include<vector> // for vector 
using namespace std; 
int main() 
{ 
    // Initializing vector 
    vector<int> vect = { 2, 1, 5, 6, 8, 7 }; 
      
    // partitioning vector using stable_partition() 
    // in sorted order 
    stable_partition(vect.begin(), vect.end(), [](int x) 
    { 
        return x%2 == 0;         
    }); 
      
    // Displaying partitioned Vector 
    cout << "The partitioned vector is : "; 
    for (int &x : vect) cout << x << " "; 
    cout << endl; 
      
    // Declaring iterator 
    vector<int>::iterator it1; 
      
    // using partition_point() to get ending position of partition 
    auto it = partition_point(vect.begin(), vect.end(), [](int x) 
    { 
        return x%2==0; 
    }); 
      
    // Displaying partitioned Vector 
    cout << "The vector elements returning true for condition are : "; 
    for ( it1= vect.begin(); it1!=it; it1++) 
    cout << *it1 << " "; 
    cout << endl; 
      
    return 0; 
      
} 

Output:

The partitioned vector is : 2 6 8 1 5 7 
The vector elements returning true for condition are : 2 6 8 

Example 4

Let's see another simple example:

#include <iostream> // std::cout 
#include <algorithm> // std::partition, std::partition_point 
#include <vector> // std::vector 

using namespace std;
  
bool IsNegative(int i) { return (i < 0); } 
  
int main() 
{ 
    vector<int> data{ 1, -1, 3, -4, 5, 2, -2, 4, -5, -3 }; 
    vector<int> negative, positive; 
  
    // partition data on the basis of odd elements using  
    // pred IsNegative() 
    stable_partition(data.begin(), data.end(), IsNegative); 
  
    // gets the partition point based on odd elements 
    auto it = partition_point(data.begin(), data.end(),  IsNegative); 
  
    // assign elements to odd from beginning till 
    // partition point. 
    negative.assign(data.begin(), it); 
    positive.assign(it, data.end()); 
  
    // print contents of odd: 
    cout << "Negative: "; 
    for (int& x : negative) 
        cout << ' ' << x; 
    cout << '\n'; 
  
    // print contents of even: 
    cout << "Positive: "; 
    for (int& x : positive) 
        cout << ' ' << x; 
    cout << '\n'; 
  
    return 0; 
}

Output:

Negative:  -1 -4 -2 -5 -3
Positive:  1 3 5 2 4

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