By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,194 Members | 884 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,194 IT Pros & Developers. It's quick & easy.

getting list<string> sort() to use my operator< ??

P: n/a
Hi,

I have a definition
bool operator<(string s_s, string s_t)
{
....
}

and a variable
list<string> concomp;

but when i sort the list
concomp.sort();

it does NOT call the above operator, but rather uses predefined
lexical ordering.

I'm thinking the < might need to be defined differently?
(as g++ already has a < for strings and i need to override it somehow)

I'm using gcc3.2.2 on red hat/mandrake.

Thanks,
-Gill
Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
"gbgbgbgb" <p_******@surfy.net> wrote in message
news:7c**************************@posting.google.c om...
I have a definition
bool operator<(string s_s, string s_t)
{
...
} The standard library already defines this operator
(in the std namespace). You cannot override the
standard operator like this.
and a variable
list<string> concomp;

but when i sort the list
concomp.sort();

There is an overload of the 'sort' member function that
takes a function object to specify the ordering.

Here's an example, using a simple function instead of
a function object:

#include <string>
#include <list>

bool myOrdering( std::string const& a
, std::string const& b )
{
return a<b; // or some other op
}

void mySort(std::list<std::string>& v)
{
v.sort( &myOrdering );
}
hth, Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- e-mail contact form
Jul 22 '05 #2

P: n/a
"Ivan Vecerina" <pl*****************@ivan.vecerina.com> wrote


hth, Ivan


It did, indeed.
Thanks a lot, Ivan!
Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.