![]() |
|
#1
|
|||
|
|||
Partition algorithmHi,
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
|
|||
|
|||
Re: partition algorithmQuote:
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:
Now, however you need a predicate with two arguments. Well the function is easy enough: CPP / C++ / C Code:
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:
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
|
|||
|
|||
Re: Partition algorithmThis seems to be a quick sort algorithm.
|
|
#4
|
|||
|
|||
Re: Partition algorithmQuote:
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
|
|||
|
|||
Re: Partition algorithmThanks for your explanation.
|
Recent GIDBlog
Once again, no time for hobbies by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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