C++ std::partition_point with multiset














































C++ std::partition_point with multiset



Implementable on C++11/14/17

Description:

partition_point( ) is a Standard Template Library function under the <algorithm> header 
which returns a iterator pointing the position where the partition is present in an already partitioned container.

partition_point( ) Definition:

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

Return type: returns an iterator indicating the point of partition.

Working: The function partition_point() takes in 3 parameters, start position and end position iterators of the container and 
                  a predicate function which returns a boolean value (true or false).The function traverses the container from first
                  position to last, up till the point where the first value for which the predicate return value is false is encountered
                  Once encountered, iterator pointing to that location is returned.
 
Container used here:- std::multiset. Multisets are associative containers that can have multiple elements with same values.

Implementation:

#include <iostream>
#include <set>
#include <algorithm>

using namespace std;

//Predicate function to check whether the character element is an uppercase letter or not
bool isUpperCase(char ch)
{
	if(isupper(ch))
		return true;

	return false;
}

//Driver
int main(int argc, char const *argv[])
{
   multiset<char> arr{'A', 'B', 'C', 'D', 'e', 'f', 'g', 'h'};    //The first container, already partitioned

   multiset<char> :: iterator it = partition_point(arr.begin(), arr.end(), isUpperCase);	//Finding the partition point

   //printing the two partitions

   cout<<"UpperCase letters:"<<endl;
   for(auto beg = arr.begin() ; beg != it ; beg++)
   	cout<<*beg<<" ";

   cout<<endl;

   cout<<"LowerCase letters:"<<endl;
   for(auto beg = it ; beg != arr.end() ; beg++)
   	cout<<*beg<<" ";

   cout<<endl;

   return 0;
}

Sample Output:
gs_katti@gsk-47534B:~/Desktop/CppSecret/algorithm_header$ ./a.out
UpperCase letters:
A B C D 
LowerCase letters:
e f g h 

More Articles of GHANASHYAM KATTI:

Name Views Likes
C++ std::reverse with std::vector 747 24
C++ std::search with std::vector 704 15
C++ std::is_permutation with std::vector 767 20
C++ program to reverse a path in binary search tree using queue 846 17
C++ std::partial_sort with std::vector 684 23
C++ std::partition_point with vector 717 15
C++ std::partition_point with multiset 795 27
C++ program to calculate tilt of binary tree 603 16
C++ program to find right sibling of a binary tree with parent pointers 1108 16
C++ program to find root of the tree where children id sum for every node is given 789 21
C++ std::count with std::vector 796 23
C++ std::mismatch with std::deque 695 26
C++ std::partial_sort with std::array 859 12
C++ program to remove nodes on root to leaf paths of length < k 593 20
C++ std::mismatch with std::array 1049 15
C++ std::is_permutation with std::array 640 14
C++ std::nth_element with std::array 988 23
C++ std::count_if with std::list 811 15
C++ std::move_backward with std::deque 723 12
C++ std::mismatch with std::list 683 19
C++ std::move_backward with std::list 791 20
C++ std::random_shuffle with std::array 640 24
C++ std::mismatch with std::forward_list 630 19
C++ std::count with std::array 875 26
C++ program to find simple recursive solution to check whether binary search tree contains dead end 913 19
C++ program to find ways to color a skewed tree such that parent and child have different colors 774 28
C++ std::count_if with std::array 953 23
C++ program to find deepest left leaf node in a binary tree using recursion 525 20
C++ program to check if a given binary tree is sumtree 620 11
C++ program to calculate size of a tree using recursion 863 11
C++ program to transform a binary search tree to greater sum tree 582 14
C++ program to find longest path with same values in a binary tree 1115 11
C++ program to find extract leaves of a binary tree in a doubly linked list 597 17
C++ program to find sum of nodes at k-th level in a tree represented as string 979 16
C++ std::count_if with std::multiset 870 17
C++ std::count with std::deque 2218 20
C++ program to find all duplicate subtrees 790 15
C++ program to find mirror of a given node in binary tree 703 26
C++ program to find deepest right leaf node in a binary tree using recursion 636 17
C++ std::mismatch with std::vector 706 19
C++ program to find maximum width of a binary tree 761 14
C++ std::partition_point with std::array 670 16
C++ program to find distance from root to given node in a binary tree 605 14
C++ program to find factor tree of a given number 1341 16
C++ std::partition_point with std::list 999 13
C++ std::reverse with std::deque 1154 21
C++ program to remove all nodes which don%u2019t lie in any path with sum>= k 669 17
C++ std::count with std::list 649 11
C++ std::partition_point with std::deque 736 20
C++ std::count_if with std::deque 838 26
C++ std::partial_sort with std::deque 766 13
C++ std::mismatch with std::multiset 723 21
C++ std::search with std::deque 680 20
C++ std::partition_point with std::forward_list 651 14
C++ std::count_if with std::vector 901 16
C++ program to find largest subtree having identical left and right subtrees 646 14
C++ std::random_shuffle with std::deque 1442 15
C++ std::nth_element with std::vector 776 19
C++ std::move_backward with std::array 841 20
C++ program to find a number in minimum steps 700 23
C++ std::reverse with std::array 791 18
C++ std::random_shuffle with std::vector 733 14
C++ program to check if all leaves are at same level 571 15
C++ std::count with std::multiset 521 14
C++ program to connect nodes at same level 595 21
C++ std::move_backward with std::vector 739 20
C++ std::is_permutation with std::deque 597 13
C++ std::search with std::array 767 17
C++ std::nth_element with std::deque 1655 18
C++ program to find longest consecutive sequence in binary tree 652 11

Comments