473,770 Members | 4,999 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

std:sort predicate function optional arguments

hi !

Can the predicate function used in std::sort take optional
arguments ? For instance. I have a class Point. I create a vector of
this and then want to compare the slopes of these points with respect
to another point of the same class (Graham's convex hull algorithm
anyone ?)

ganesh
Sep 29 '08 #1
5 3186
On 9ÔÂ29ÈÕ, ÏÂÎç6ʱ01·Ö, Ganesh <ganesh.i...@gm ail.comwrote:
hi !

Can the predicate function used in std::sort take optional
arguments ? For instance. I have a class Point. I create a vector of
this and then want to compare the slopes of these points with respect
to another point of the same class (Graham's convex hull algorithm
anyone ?)

ganesh
you can provide your own predicate.

class cmp {
public:
cmp(...) {
// init thePoint_
}
bool operator() (point const& lh, point const& rh) {
// ...
}
private:
point thePoint_;
};
Sep 29 '08 #2
Xiaobin Huang wrote:
you can provide your own predicate.

class cmp {
public:
cmp(...) {
// init thePoint_
}
bool operator() (point const& lh, point const& rh) {
// ...
}
private:
point thePoint_;
};
Nitpicking, but isn't that called a comparator? If is it *also* a
predicate?
Sep 29 '08 #3
On 2008-09-29 13:45:24 -0400, Juha Nieminen <no****@thanks. invalidsaid:
Xiaobin Huang wrote:
>you can provide your own predicate.

class cmp {
public:
cmp(...) {
// init thePoint_
}
bool operator() (point const& lh, point const& rh) {
// ...
}
private:
point thePoint_;
};

Nitpicking, but isn't that called a comparator? If is it *also* a
predicate?
There is a version of std::sort that takes a predicate. It doesn't care
whether you call it a comparator, a functoid, or fred. But when you're
talking about std::sort, if you refer to a predicate by some other
name, you run the risk of confusing your listener.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Sep 29 '08 #4
On Sep 29, 9:51 pm, Pete Becker <p...@versatile coding.comwrote :
On 2008-09-29 13:45:24 -0400, Juha Nieminen <nos...@thanks. invalidsaid:
Xiaobin Huang wrote:
you can provide your own predicate.
class cmp {
public:
cmp(...) {
// init thePoint_
}
bool operator() (point const& lh, point const& rh) {
// ...
}
private:
point thePoint_;
};
Nitpicking, but isn't that called a comparator? If is it
*also* a predicate?
There is a version of std::sort that takes a predicate. It
doesn't care whether you call it a comparator, a functoid, or
fred. But when you're talking about std::sort, if you refer to
a predicate by some other name, you run the risk of confusing
your listener.
While it's obviously a predicate; any functional object which
returns bool is a predicate. The standard never uses the word
predicate for it, however, and calls it Compare. It also
imposes a number of additional constraints on it which don't
apply to predicates in general. In fact, it even says that "A
sequence is sorted with respect to a comparator comp if [...]",
using the very same word as Juha. So while it is a predicate,
I'd consider "comparator " a more precise word. (All comparators
are predicates, but not all predicates are comparators. And
here, we need a predicate which is a comparator.)

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Sep 30 '08 #5
On 2008-09-30 05:16:36 -0400, James Kanze <ja*********@gm ail.comsaid:
On Sep 29, 9:51 pm, Pete Becker <p...@versatile coding.comwrote :
>On 2008-09-29 13:45:24 -0400, Juha Nieminen <nos...@thanks. invalidsaid:
>>Xiaobin Huang wrote:
you can provide your own predicate.
>>>class cmp {
public:
cmp(...) {
// init thePoint_
}
bool operator() (point const& lh, point const& rh) {
// ...
}
private:
point thePoint_;
};
>>Nitpicking, but isn't that called a comparator? If is it
*also* a predicate?
>There is a version of std::sort that takes a predicate. It
doesn't care whether you call it a comparator, a functoid, or
fred. But when you're talking about std::sort, if you refer to
a predicate by some other name, you run the risk of confusing
your listener.

While it's obviously a predicate; any functional object which
returns bool is a predicate. The standard never uses the word
predicate for it, however, and calls it Compare. It also
imposes a number of additional constraints on it which don't
apply to predicates in general. In fact, it even says that "A
sequence is sorted with respect to a comparator comp if [...]",
using the very same word as Juha. So while it is a predicate,
I'd consider "comparator " a more precise word. (All comparators
are predicates, but not all predicates are comparators. And
here, we need a predicate which is a comparator.)
You're right, of course. Sorting and partitioning rely on comparators
(which take two arguments), and most other algorithms rely on
predicates (which take one argument). I should know better than to post
while I'm in the middle of post-meeting editing.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Sep 30 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
3504
by: lok | last post by:
i have a class: template <class T1, class T2> class CPairMapping { public: typedef std::pair<T1, T2> ValuePair_t; typedef std::vector<ValuePair_t> ValueList_t; typedef std::binary_function< ValuePair_t, ValuePair_t, bool> ValuePair_IsLess; void SortAscend(const ValuePair_IsLess& isLess_) {
6
2340
by: alexhong2001 | last post by:
Does "std::sort" work only with sequence containers, not associative containers at all? Among sequential containers, can it be used with "list", "queue" and other sequence containers besides "vector"? Are "istringstream" and "ostringstream" covered in the book, "STL Tutorial and Reference" (second edition) by D. Musser, et al.? Seems like I could not find any related topic in the book.
9
13105
by: JasBascom | last post by:
say i had 97456 and 23456 is there already a sort function to check which one begins with the smaller number rearrange it. in this case the bottom number should clearly be rearranged to the top as it lower in numerical order.
4
3350
by: hall | last post by:
I accidently overloaded a static member function that I use as predicate in the std::sort() for a vector and ended up with a compiler error. Is this kind of overload not allowed for predicates and if so, why not? Shouldn the compiler be able to tell which of he overloaded functions to use? The second A::comp() is the one I accidently added and gives the error message (in Borland C++Builder 6) Unit1.cpp E2285 Could not find a match for
7
3548
by: Ireneusz SZCZESNIAK | last post by:
I want to sort a vector with the std::sort function. There are two functions: one with two arguments, the other with three arguments. I am using the one with three arguments. I noticed that there is a huge overhead of using this function, which comes from copying the function object, i.e., the object that compares the elements of the vector, which is passed to the function not by reference but by value. Now, at the end of this mail...
15
3342
by: Peter Olcott | last post by:
Does anyone know how to do this?
5
3909
by: fade | last post by:
Good afternoon, I need some advice on the following: I've got a class that has a member std::vector<CStringm_vFileName and a member CString m_path; The vector contains a bunch of filenames with no path included (no C:\...) eg: my_file2.jpg, my_file1.bmp, etc... and m_path stores the path, eg: C:\folder1 I want to sort this vector according to different criterion, such as
1
5171
by: mscava | last post by:
How can I make this construction valid? It still gives me error about no matching function std::sort(...). I made a little search and wrong thing is probably the predicate... template <typename TT Polygon<T:: Height() const { class BinPred : public std::binary_function< Vector2D<T>, Vector2D<T>, bool > {
0
10231
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10005
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9871
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7416
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6679
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5313
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5452
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3972
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3576
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.