TheDeveloperBlog.com

Home | Contact Us

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

C++ algorithm is_partitioned() function

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

C++ Algorithm is_partitioned() is used to test to see if a range [first, last) is partitioned according to a predicate. In other words, all the elements in the range that satisfies the predicate are at the beginning of the sequence.

If the range is empty then it returns true.

Syntax

template <class InputIterator, class UnaryPredicate>
  bool is_partitioned (InputIterator first, InputIterator last, UnaryPredicate pred);

Parameter

first: An input iterator pointing to the first element in the range.

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

pred: A user-defined unary predicate function that returns true for the elements expected to be found in the beginning of the range.

Return value

This function returns true if the range is empty or is partitioned by given predicate pred, otherwise it returns false.

Complexity

Complexity is linear in the range [first, last): calls pred for each element until a mismatch is found.

Data races

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

Each element is accessed exactly once.

Exceptions

This function throws an exception if either pred or an operation on iterator throws an exception.

Please note that invalid parameters cause an undefined behavior.

Example 1

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

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
  vector<int> v = {1, 2, 3, 4, 5};
  
  cout<<"Before Partition: ";
    for_each(v.begin(), v.end(), [](int v) {
   cout << v << " ";
  });


  auto pred = [](int x) { return x % 2 == 0; };

  // Divide it into an even group and an odd group 
  partition(v.begin(), v.end(), pred);
  cout<<"\nAfter partition : ";
  for_each(v.begin(), v.end(), [](int x) {
   cout << x << " ";
  });

  cout<<"\n\nIs it partitioned?"<<endl;
  // Is it divided into an even group and an odd group?  
  if (is_partitioned(v.begin(), v.end(), pred)) {
    cout << "Yes,It is Partitioned" << endl;
  }
  else {
    cout << "No,It is not Partitioned" << endl;
  }
  
  return 0;
}

Output:

Before Partition: 1 2 3 4 5 
After partition : 4 2 3 1 5 

Is it partitioned?
Yes,It is Partitioned

Example 2

Let's see another simple example:

#include <iostream> 
#include <algorithm> 
#include <vector> 
  
// Defining the BinaryFunction 
bool pred(int a) 
{ 
    return (a % 2 == 0); 
} 
  
using namespace std; 
int main() 
{ 
    // Declaring first vector 
    vector<int> v1 = { 2, 4, 6, 3, 5, 7, 9 }; 
  
    // Using std::is_partitioned 
    bool b = std::is_partitioned(v1.begin(), v1.end(), pred); 
  
    if (b == 1) { 
        cout << "All the even no. are present before odd no."; 
    } else { 
        cout << "All the even no. are not present before odd no."; 
    } 
  
    // Inserting an even no. at the end of v1 
    // so std::is_partitioned returns false 
    v1.push_back(16); 
  
    // Now again using std::is_partitioned 
    b = std::is_partitioned(v1.begin(), v1.end(), pred); 
  
    if (b == 1) { 
        cout << "\nAll the even no. are present before odd no."; 
    } else { 
        cout << "\nAll the even no. are not present before odd no."; 
    } 
  
    return 0; 
}

Output:

All the even no. are present before odd no.
All the even no. are not present before odd no.

Example 3

Let's see another simple example:

#include <algorithm>
#include <array>
#include <iostream>
 
int main()
{
    std::array<int, 9> v = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
 
    auto is_even = [](int i){ return i % 2 == 0; };
    std::cout.setf(std::ios_base::boolalpha);
    std::cout << std::is_partitioned(v.begin(), v.end(), is_even) << ' ';
 
    std::partition(v.begin(), v.end(), is_even);
    std::cout << std::is_partitioned(v.begin(), v.end(), is_even) << ' ';
 
    std::reverse(v.begin(), v.end());
    std::cout << std::is_partitioned(v.begin(), v.end(), is_even);
}

Output:

false true false 

Example 4

Let's see another simple example:

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
  vector<int> v = {11, 2, 3, 4, 15, 12};
  
  cout<<"Before Partition: ";
    for_each(v.begin(), v.end(), [](int v) {
   cout << v << " ";
  });


  auto pred = [](int x) { return x < 10; };
 
  // Divide it into an even group and an odd group 
  partition(v.begin(), v.end(), pred);
  cout<<"\nAfter partition: ";
  for_each(v.begin(), v.end(), [](int x) {
   cout << x << " ";
  });

  cout<<"\n\nIs it partitioned?"<<endl;
  // Is it divided into an even group and an odd group?  
  if (is_partitioned(v.begin(), v.end(), pred)) {
    cout << "Yes, It is Partitioned." << endl;
  }
  else {
    cout << "No, It is not Partitioned." << endl;
  }
  
  return 0;
}

Output:

Before Partition: 11 2 3 4 15 12 
After partition: 4 2 3 11 15 12 

Is it partitioned?
Yes, It is Partitioned.

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