C++ std::is_partitioned with std::vector














































C++ std::is_partitioned with std::vector



 This article is about the is_partition() algorithm function with std::vector container. It
 is an STL algorithm in <algorithm> header file.  
 is_partition() can take one form:
    template <class InputIterator, class UnaryPredicate>
bool is_partitioned (InputIterator first, InputIterator last, UnaryPredicate pred);
Here the range used is [first, last). The first positioned element is included whereas the last positioned element is not used. It checks whether the elements in range which satisfies
 the pred function preceds the elements not satisfying it. It returns true if the list is 
 empty.
 
 Program:
 To check if a set of elements is partitioned by pred prime.

#include<iostream>
#include<algorithm> // for partition
#include<vector> // for vector
#include<cmath> // for sqrt

using namespace std;

// to check if a given number is prime or not
bool prime(int a)
{
int count=0,i;

for(i=2;i<=sqrt(a);i++)
if(a%i==0)
count++;

return (count==0);
}

int main()
{
// vector to store all the elements
vector <int> v;
int num,i,n;

cout<<"Enter the number of elements: ";
cin>>num;

cout<<"Enter the elements: ";
for(i=0;i<num;i++)
{
cin>>n;
v.push_back(n);
}

if(is_partitioned(v.begin(), v.end(), prime))
cout<<"Partioned Set..";
else
cout<<"Non-Partioned Set..";
}

Comments