Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Class requirements to be used in a STL container?

Reply
Thread Tools

Class requirements to be used in a STL container?

 
 
Rui Maciel
Guest
Posts: n/a
 
      05-27-2009
What requirements should a class fulfil in order to be able to be used with a STL container class?


Thanks in advance,
Rui Maciel

 
Reply With Quote
 
 
 
 
Neelesh
Guest
Posts: n/a
 
      05-27-2009
On May 27, 3:14*pm, Rui Maciel <(E-Mail Removed)> wrote:
> 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<

 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      05-28-2009
On May 27, 7:55 pm, Neelesh <(E-Mail Removed)> wrote:
> On May 27, 3:14 pm, Rui Maciel <(E-Mail Removed)> wrote:


> > 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<


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.)

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


 
Reply With Quote
 
SG
Guest
Posts: n/a
 
      05-28-2009
On 27 Mai, 19:55, Neelesh <(E-Mail Removed)> wrote:
>
> > 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Copy elements from one STL container to another STL container Marko.Cain.23@gmail.com C++ 4 02-16-2006 05:03 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
What is the name of sorting algorithm used in list (STL) class? Wahoo C++ 5 05-19-2004 10:22 AM
Web page for STL container performace requirements? Stephan Br?nnimann C++ 2 11-10-2003 03:26 PM
To STL or not to STL Allan Bruce C++ 41 10-17-2003 08:21 PM



Advertisments