Passing different sort methods to multiset

D

Dale Marchand

I'm trying to use an object in two different multiset containers, each
with it's own sort method. For the most frequently used, I overrode
the operator< method in the class, and for the second I wrote a method
LocationSort(). I can't seem to arrive upon the correct syntax for
defining a multiset that will use LocationSort.

multiset<DotObject,DotObject::LocationSort> mySet;

fails.

Is there a correct way to do this while keeping LocationSort inside of
my class? I know I could wrap it in a separate structure and pass in
the structure name, but would prefer to keep it inside the class code
for ease-of-maintenance reasons.

Thanks.

Dale
 
H

Heinz Ozwirk

Dale Marchand said:
I'm trying to use an object in two different multiset containers, each
with it's own sort method. For the most frequently used, I overrode
the operator< method in the class, and for the second I wrote a method
LocationSort(). I can't seem to arrive upon the correct syntax for
defining a multiset that will use LocationSort.

multiset<DotObject,DotObject::LocationSort> mySet;

fails.

Is there a correct way to do this while keeping LocationSort inside of
my class? I know I could wrap it in a separate structure and pass in
the structure name, but would prefer to keep it inside the class code
for ease-of-maintenance reasons.

Try to make LocationSort a static member function (with two parameters) of
DotObject. If that doesn't work (I didn't try it), you can still make a
separate struct/class. Such a struct can be defined inside the DotObject
class, like

class DotObject
{
public:
struct LocationSort
{
bool operator()(DotObject const& lhs, DotObject const& rhs)
const
{
return ...
}
};
...
};

HTH
Heinz
 
D

Dale Marchand

Thanks. I just hit upon option 2 about 10 seconds prior to reading
your post (no, really I did!). It works fine....I'd just think there'd
be a simpler, more elegent way.

I'd prefer to stay away from static members, just because I've got a
bunch of threads running and prefer to stay away from the synch hassles
that static's bring.

Thanks.

Dale
 
D

David Harmon

On 3 Jan 2006 13:11:24 -0800 in comp.lang.c++, "Dale Marchand"
I'd just think there'd be a simpler, more elegent way.
I'd prefer to stay away from static members

The compare function is called with two arguments, and not as a
member function. If you want to use a member function, you can wrap
it in a std::mem_fun() adapter. See Stroustrup chap. 18.4.4.2
just because I've got a
bunch of threads running and prefer to stay away from the synch hassles
that static's bring.

No reason a static member _function_ (without any local static
variables etc.) should cause a problem.
 
D

Dale Marchand

I read your reference on mem_fun, and it looks like I'm limited to 1
arg member functions at most. Since the Compare function requires 2
args, I don't see how I can use that, unless I'm missing something?
 
D

David Harmon

On 4 Jan 2006 10:31:43 -0800 in comp.lang.c++, "Dale Marchand"
I read your reference on mem_fun, and it looks like I'm limited to 1
arg member functions at most. Since the Compare function requires 2
args, I don't see how I can use that, unless I'm missing something?

What you are missing is that in a member function, the object upon
which the function is invoked is equivalent to another argument.
Foo.memfun(Bar) and nonmemfun(Foo, Bar) have effectively the same
number of arguments.
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top