how to use not1?

J

John Black

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?
 
P

Pete C.

John said:
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
 
T

tom_usenet

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
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
 
J

John Harrison

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
 
T

tom_usenet

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
 
M

Michiel Salters

John Harrison said:
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
 
R

Rob Williscroft

Michiel Salters wrote in
in comp.lang.c++:
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::eek:stream_iterator< char >( std::cout, ", " )
);
}

Works for Me, am I missing something ?.

Rob.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,190
Latest member
ClayE7480

Latest Threads

Top