GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 03-Jun-2008, 10:25
acosgaya acosgaya is offline
New Member
 
Join Date: Jun 2006
Posts: 13
acosgaya is on a distinguished road

Partition algorithm


Hi,

I have a std::vector of std:: pair<int, int> elements. How can I create a predicate function for the stl partition algorithm in this vector, using the "first" value in the pair to compare, and passing as a parameter the pivot value.

I want elements greater than the pivot value to be on left side, and elements less than or equal to the pivot value to be the right. The pivot value might not actually be in the vector.

I'd appreciate your help in this.

thanks
  #2  
Old 03-Jun-2008, 14:39
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,217
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: partition algorithm


Quote:
Originally Posted by acosgaya
Hi,

I have a std::vector of std:: pair<int, int> elements. How can I create...

If you had a boolean function whose return value depends on whether the first element of a pair is less than or equal to a particular number, it would be easy, right? (I changed the problem statement a little.)

CPP / C++ / C Code:
#include <vector>
#include <functional>
#include <algorithm>

using namespace std;
//
// First element of a pair is less than or equal to 3
//
bool firstLE3(pair<int,int> p)
{
    return (p.first <= 3);
}
.
.
.
int main()
{
    vector<pair<int,int> > vp;
.
.   // Create some pairs and put them into vp
.
    partition(vp.begin(), vp.end(), firstLE3);
.
.
.

Now, however you need a predicate with two arguments.

Well the function is easy enough:
CPP / C++ / C Code:
//
// First element of a pair is less than or equal to n
//
bool firstLEn(pair<int,int> p, int  n)
{
    return (p.first <= n);
}

But the predicate for std::partition must be a pointer to a function that takes one argument (a pair<int,int> in this case), and there is no way to send a second argument.

Or is there???

Well, yes. Yes there is...

There is an "adapter" function named bind2nd() that takes a pointer to a function with two parameters, and binds the second argument of the function being pointed to with the argument that you give to bind2nd. In other words, it wraps a single-argument function around a two-argument function. I am always amazed when I run across little things like this in C++. I mean, my flabber is gasted. Totally.

Here's how you can use it:

CPP / C++ / C Code:
    int pivot;  // The partition will be based on this
.
.
.
    pivot = 3; // Or from user input or whatever...
.
.
.
    partition(vp.begin(), vp.end(), bind2nd(ptr_fun(firstLEn), pivot));

This calls the function firstLEn, with its second argument equal to pivot

The pointer has to be cast to a ptr_fun type since bind2nd is a template function and you have to tell the compiler that you are feeding it a pointer to a function---rather than, say an operator---so that the proper function can be generated.

Regards,

Dave
Last edited by davekw7x : 03-Jun-2008 at 15:27.
  #3  
Old 04-Jun-2008, 02:31
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 545
Peter_APIIT can only hope to improve

Re: Partition algorithm


This seems to be a quick sort algorithm.
  #4  
Old 04-Jun-2008, 09:04
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,217
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Partition algorithm


Quote:
Originally Posted by Peter_APIIT
This seems to be a quick sort algorithm.

The topic of the thread is an illustration of a way to use the std::partition algorithm in a way that some textbooks and other reference materials (apparently) don't exactly spell out.

The std::partition function used this way might be helpful if you wanted to show how to implement a quicksort algorithm or other function that uses partitioning based on size of elements relative to some variable. This might be for teaching purposes or for any other reason that you didn't want to use the std::sort algorithm.

In fact, there are many problems other than sorting that need some kind of partitioning. The std::partition algorithm is one of those things in the C++ STL toolbox that doesn't mean much to beginners (and not-so-beginners also) unless you get to the point where you need it.

Regards,

Dave
  #5  
Old 06-Jun-2008, 06:07
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 545
Peter_APIIT can only hope to improve

Re: Partition algorithm


Thanks for your explanation.
 
 

Recent GIDBlogOnce again, no time for hobbies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
No partition in mirror disk Firemonkey65 Computer Hardware Forum 2 09-Mar-2008 16:42
Hard drive/CPU Diagnoses Issues binarybug Computer Hardware Forum 1 22-Jan-2007 20:23
partition a vector of vectors acosgaya C++ Forum 0 06-Oct-2006 10:27
Algorithm Help Please! daking_09 C++ Forum 0 24-May-2006 20:12
Quick, Insertion, and Partition silicon C++ Forum 0 18-May-2005 21:49

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 06:26.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.