associative containers: requirements

N

Neil Zanella

Hello,

I would like to know what the exact requirements are for classes that use
associative containers in terms of which overloaded operators and
constructors they must provide. For instance, if I have a class
called Foo, and declare and std::map<Foo>, then what other
methods and constructors must Foo implement for
std::map<Foo> to work as expected besides

bool operator<(const Foo &foo) const;

Thanks,

Neil
 
V

Victor Bazarov

Neil Zanella said:
I would like to know what the exact requirements are for classes that use
associative containers in terms of which overloaded operators and
constructors they must provide. For instance, if I have a class
called Foo, and declare and std::map<Foo>, then what other
methods and constructors must Foo implement for
std::map<Foo> to work as expected besides

bool operator<(const Foo &foo) const;

Actually, that is not even a requirement. You may supply your own
comparator for the keys. The requirement for the comparator 'comp',
though, is that in order for two keys 'k1' and 'k2' to be _equivalent_,
comp(k1,k2) should be false _as_well_as_ comp(k2,k1).

BTW, you cannot declare std::map<Foo> because std::map needs at least
two template arguments.

Get yourself a copy of Nicolai Josuttis' "The C++ Standard Library".
It will be the best book you've used. Trust me.

Victor
 

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,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top