Class requirements to be used in a STL container?

R

Rui Maciel

What requirements should a class fulfil in order to be able to be used with a STL container class?


Thanks in advance,
Rui Maciel
 
N

Neelesh

What requirements should a class fulfil in order to be able to be used with a STL container class?

Copy constructible and Assignable are the two requirements for all
container classes in general. Further, if the container is associative
container, then the members (for set) and keys (for map) must have
strict weak ordering. The comparsion operator can be explicitly
provided. If not, it defaults to operator<
 
J

James Kanze

Copy constructible and Assignable are the two requirements for
all container classes in general. Further, if the container is
associative container, then the members (for set) and keys
(for map) must have strict weak ordering. The comparsion
operator can be explicitly provided. If not, it defaults to
operator<

Strictly speaking, the comparison operator defaults to
std::less<T> (for type T). Which in turn defaults to operator<
for non-pointer types, and to something which provides the
necessary ordering for pointer types. (Often operator<: while
comparing pointers not pointing into the same object is
undefined in the standard, many implementations do define it.)
 
S

SG

What requirements should a class fulfil in order to be able to be
used with a STL container class?

Copy constructible and Assignable are the two requirements for all
container classes in general. [...]

In a map the mapped_type also needs to be default-constructible when
you want to use its operator[] function. But if you don't use member
functions that require the type to be default-constructible then a non-
default-constructible will do just fine. So, you basically have a
minimum set of requirements you need to satify to make the class
template work and a couple of extra requirements for special
operations that you might not need.

It's interesting to note that in C++0x the minimum requirements don't
include CopyConstructible or even MoveConstructible in some cases
(std::list).

Cheers!
SG
 

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,994
Messages
2,570,223
Members
46,814
Latest member
SpicetreeDigital

Latest Threads

Top