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

G

gbgbgbgb

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
 
I

Ivan Vecerina

gbgbgbgb said:
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
 

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
474,151
Messages
2,570,854
Members
47,394
Latest member
Olekdev

Latest Threads

Top