473,320 Members | 1,965 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

how to use not1?

Hi,
I have such a vector, vector<MyClass> vec and here is how I want to
sort it,

sort(vec.begin(), vec.end(), Util::MyClassOrder);

Now in another code I want to get opposite order sort, so I write
this way,

sort(vec.begin(), vec.end(), not1(Util::MyClassOrder));

But it does not compile!

The signature of MyClassOrder is like,

class Util{
static bool MyClassOrder(MyClass&, MyClass&);
};

Any obvious reason?

Jul 22 '05 #1
6 2573
John Black wrote:
Hi,
I have such a vector, vector<MyClass> vec and here is how I want to
sort it,

sort(vec.begin(), vec.end(), Util::MyClassOrder);

Now in another code I want to get opposite order sort, so I write
this way,

sort(vec.begin(), vec.end(), not1(Util::MyClassOrder));

But it does not compile!

The signature of MyClassOrder is like,

class Util{
static bool MyClassOrder(MyClass&, MyClass&);
};

Any obvious reason?


You need not2. not2 takes a binary predicate (like yours), not1 takes an
unary one.

- Pete
Jul 22 '05 #2
On Mon, 14 Jun 2004 17:53:43 -0600, John Black <bl***@eed.com> wrote:
Hi,
I have such a vector, vector<MyClass> vec and here is how I want to
sort it,

sort(vec.begin(), vec.end(), Util::MyClassOrder);

Now in another code I want to get opposite order sort, so I write
this way,

sort(vec.begin(), vec.end(), not1(Util::MyClassOrder));

But it does not compile!
It also isn't correct. Assuming MyClassOrder is a strict weak
ordering, !MyClassOrder will *not* be a strict weak ordering, so
you'll get weird crashes if you try to use it. Remember, the negation
of < is not >, but >=.
The signature of MyClassOrder is like,

class Util{
static bool MyClassOrder(MyClass&, MyClass&);
Ordering comparators should take the arguments by const reference,
since they aren't going to modify the elements.
};

Any obvious reason?


Apart from needing std::not2, that's the wrong approach. Instead, you
need to reverse the arguments. Use boost::bind:

sort(vec.begin(), vec.end(),boost::bind(&Util::MyClassOrder, _2, _1));

(note I bind _2 to the first parameter, _1 to the second, to reverse
the sort order).

See www.boost.org.

Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #3
>
Apart from needing std::not2, that's the wrong approach. Instead, you
need to reverse the arguments. Use boost::bind:

sort(vec.begin(), vec.end(),boost::bind(&Util::MyClassOrder, _2, _1));

(note I bind _2 to the first parameter, _1 to the second, to reverse
the sort order).


How about using reverse iterators?

sort(vec.rbegin(), vec.rend(), Util::MyClassOrder);

Just a thought, I haven't checked it or anything.

john
Jul 22 '05 #4
On Tue, 15 Jun 2004 11:52:31 +0100, "John Harrison"
<jo*************@hotmail.com> wrote:

Apart from needing std::not2, that's the wrong approach. Instead, you
need to reverse the arguments. Use boost::bind:

sort(vec.begin(), vec.end(),boost::bind(&Util::MyClassOrder, _2, _1));

(note I bind _2 to the first parameter, _1 to the second, to reverse
the sort order).


How about using reverse iterators?

sort(vec.rbegin(), vec.rend(), Util::MyClassOrder);

Just a thought, I haven't checked it or anything.


Good thinking!

Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #5
"John Harrison" <jo*************@hotmail.com> wrote in message news:<2j************@uni-berlin.de>...

Apart from needing std::not2, that's the wrong approach. Instead, you
need to reverse the arguments. Use boost::bind:

sort(vec.begin(), vec.end(),boost::bind(&Util::MyClassOrder, _2, _1));

(note I bind _2 to the first parameter, _1 to the second, to reverse
the sort order).


How about using reverse iterators?

sort(vec.rbegin(), vec.rend(), Util::MyClassOrder);

Just a thought, I haven't checked it or anything.


Don't need to, it it pretty obvious and quite likely the fastest solution.
It's not possible though, if all you have is two RanIt's in a
template<class RanIt> function.

Regards,
Michiel Salters
Jul 22 '05 #6
Michiel Salters wrote in
news:fc**************************@posting.google.c om in comp.lang.c++:
How about using reverse iterators?

sort(vec.rbegin(), vec.rend(), Util::MyClassOrder);

Just a thought, I haven't checked it or anything.


Don't need to, it it pretty obvious and quite likely the fastest
solution. It's not possible though, if all you have is two RanIt's in
a template<class RanIt> function.


#include <iostream>
#include <ostream>
#include <iterator>
#include <algorithm>
#include <vector>
template < typename RanIt >
void my_sort( RanIt beg, RanIt end )
{
std::sort(
std::reverse_iterator< RanIt >( end ),
std::reverse_iterator< RanIt >( beg )
);
}

int main()
{
std::vector< char > v;
for ( char const *p = "0123456789"; *p; ++p )
{
v.push_back( *p );
}

my_sort( v.begin(), v.end() );

std::copy(
v.begin(), v.end(),
std::ostream_iterator< char >( std::cout, ", " )
);
}

Works for Me, am I missing something ?.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #7

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

Similar topics

7
by: mead | last post by:
The code is from a Meyers' book... class BankAccount { ... }; // as above // new class representing accounts that bear interest class InterestBearingAccount: public BankAccount { public:...
13
by: SpOiLeR | last post by:
Hi! Let's assume I have this typedef std::list<std::string> string_list; string_list sl; If I want to traverse through this, I usually do this: string_list::iterator i = sl.begin();
5
by: dave_if | last post by:
I have a vector of TCard objects, I am sorting them based on the the box field. That part works fine (finally!). I would then like to random_shuffle all cards that are in a particular box, that is,...
10
by: SpOiLeR | last post by:
I have function bool IsGood (const std::string& sr); I want to use that function in std::not1 STL functor. I tried this: not1(IsGood) /* error : 'std::unary_negate<_Fn1> std::not1(const...
11
by: eeykay | last post by:
Hello, I am facing a starnge problem while erasing the last member in a vector. I am using VC++ .NET 2002 complier. I have vector of CComPtr<..> (irrelevant here), and then I iterate over the...
17
by: silverburgh.meryl | last post by:
In STL, there is no copy_if algorithm. But how can I emulate that? I am thinking about using find_if and then a copy. But how can I pass the InputIterator (begin and end) to copy so that it knows...
11
by: Jacek Dziedzic | last post by:
Hi! I need a routine like: std::string nth_word(const std::string &s, unsigned int n) { // return n-th word from the string, n is 0-based // if 's' contains too few words, return "" //...
7
by: markscottwright | last post by:
I'm using visual studio 8, and the following code is failing. For the life of me, I can't see what's wrong... void myTest(std::string const& in) { using namespace std; string::const_iterator...
5
by: Andrew Wingorodov | last post by:
im made subj like this: inline bool isnt_space (int sp) { return (::isspace (sp)) ? false : true; } str.erase ( std.begin () , std::find_if ( str.begin(), str.end(), isnt_space) );
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.