CPP STL Tutorial : C++ Partitions operation algorithms

Below are the functions that are used for partition operations:
is_partitioned [C++11] : Test whether range is partitioned
partition : Partition range in two
stable_partition : Partition range in two – stable ordering
partition_copy [C++11] : Partition range into two
partition_point [C++11] : Get partition point
#include <iostream>
#include <unordered_map>
#include <algorithm>    

//for more tutorials on C, C++, STL, DS visit www.ProDeveloperTutorial.com
using namespace std;

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

int main () 
{
  std::array<int,7> foo {1,2,3,4,5,6,7};

  // print contents:
  std::cout << "foo:"; 
    for (int& x:foo) 
        std::cout << ' ' << x;
        
  if ( std::is_partitioned(foo.begin(),foo.end(),IsOdd) )
    std::cout << " (partitioned)\n";
  else
    std::cout << " (not partitioned)\n";

  // partition array:
  std::partition (foo.begin(),foo.end(),IsOdd);

  // print contents again:
  std::cout << "foo:"; 
  
  for (int& x:foo)
    std::cout << ' ' << x;
    
  if ( std::is_partitioned(foo.begin(),foo.end(),IsOdd) )
    std::cout << " (partitioned)\n";
  else
    std::cout << " (not partitioned)\n";

  return 0;
}
Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *