associative containers: requirements

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Neil Zanella

    associative containers: requirements


    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

  • Victor Bazarov

    #2
    Re: associative containers: requirements

    "Neil Zanella" <nzanella@cs.mu n.ca> wrote...[color=blue]
    > 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;[/color]

    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


    Comment

    Working...