Class requirements to be used in a STL container?

Discussion in 'C++' started by Rui Maciel, May 27, 2009.

  1. Rui Maciel

    Rui Maciel Guest

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


    Thanks in advance,
    Rui Maciel
     
    Rui Maciel, May 27, 2009
    #1
    1. Advertising

  2. Rui Maciel

    Neelesh Guest

    On May 27, 3:14 pm, Rui Maciel <> 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<
     
    Neelesh, May 27, 2009
    #2
    1. Advertising

  3. Rui Maciel

    James Kanze Guest

    On May 27, 7:55 pm, Neelesh <> wrote:
    > On May 27, 3:14 pm, Rui Maciel <> 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:
    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
     
    James Kanze, May 28, 2009
    #3
  4. Rui Maciel

    SG Guest

    On 27 Mai, 19:55, Neelesh <> 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
     
    SG, May 28, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stephan Br?nnimann
    Replies:
    2
    Views:
    315
    P.J. Plauger
    Nov 10, 2003
  2. Maitre Bart
    Replies:
    2
    Views:
    539
    Maitre Bart
    Feb 11, 2004
  3. Severin Ecker

    STL-container used with references

    Severin Ecker, Apr 6, 2004, in forum: C++
    Replies:
    16
    Views:
    708
    Harald Nowak
    Apr 8, 2004
  4. Replies:
    4
    Views:
    826
    Daniel T.
    Feb 16, 2006
  5. wolverine
    Replies:
    2
    Views:
    471
    Marcus Kwok
    Jul 24, 2006
Loading...

Share This Page