changing the sorting behaviour of std::map

G

Gernot Frisch

Hi,

I know I can give a std::map a sorting function in the c'tor. However,
I would need to _change_ the sorting behaviour of an existing
instance. How would I do that?



Thank you,
 
A

Alf P. Steinbach

* Gernot Frisch:
I know I can give a std::map a sorting function in the c'tor. However,
I would need to _change_ the sorting behaviour of an existing
instance. How would I do that?

The general solution to any computer science problem is called
"indirection".

Now you're perhaps thinking of indirection in the sorting function,
passing an abstract sorting function that you can redirect, but note
that std::map has no facility for re-ordering existing elements:
changing the ordering behavior on the fly would wreak total havoc.

So what you ask for is indirection for the held instance, something that
provides a std::map interface but on the inside switches which std::map,
and copies/moves the elements over when you change the ordering.

However, whether that's what you _need_...

Most probably what you need is to examine and change the design.

Cheers,

- Alf
 
G

Gernot Frisch

The general solution to any computer science problem is called
"indirection".

Now you're perhaps thinking of indirection in the sorting function,
passing an abstract sorting function that you can redirect, but note
that std::map has no facility for re-ordering existing elements:
changing the ordering behavior on the fly would wreak total havoc.

So what you ask for is indirection for the held instance, something
that
provides a std::map interface but on the inside switches which
std::map,
and copies/moves the elements over when you change the ordering.

However, whether that's what you _need_...

Most probably what you need is to examine and change the design.

OK. I'll store data in a std::vector then and have 2 std::maps with
pointers to the elements, that sort differently.
Thank you for clearing this out,
-Gernot
 

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,755
Messages
2,569,536
Members
45,016
Latest member
TatianaCha

Latest Threads

Top