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? 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
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
> 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
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
"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
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/ This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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:...
|
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();
|
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,...
|
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...
|
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...
|
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...
|
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 ""
//...
|
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...
|
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)
);
|
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...
|
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...
|
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...
|
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...
|
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...
|
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)...
|
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...
|
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
|
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...
| |