Re: Query on STL set container

Discussion in 'C++' started by Artie Gold, Jul 19, 2003.

  1. Artie Gold

    Artie Gold Guest

    Parnakunj wrote:
    > Hi All,
    >
    > Can I use an STL set<> container to store pointers to objects and
    > ensure that it does not store to objects(different addresses) with the
    > same values for the member variables of the class.
    >
    > eg:
    >
    > #include <set> //*
    > using namespace std; //* :) To avoid noise from the group


    OK. ;-)

    >
    > class A {
    > private:
    > int a;
    > int b;
    > public:
    > A(int _a, int _b)
    > {
    > a = _a;
    > b = _b;
    > }
    >
    > }
    >
    > void main()


    NOISE. `main()' returns int...main() returns int...

    > {
    > set<A*> setA;
    > A *A1 = new A(10,20);
    > A *A2 = new A(10,20);
    >
    > setA.insert(A1);
    > setA.insert(A2);
    > }
    >
    >
    > Here the behavior that I want is that setA should only contain object
    > pointed by A1 *OR* object pointed by A2, not both.


    You'll need to define an `operator==' for the class A, as well as a
    functor that takes two A*s and compares the objects they point to -- and
    pass that functor as the second argument to the set template.

    >
    > I would appreciate your help and comments. I am a rookie with STL.
    >

    HTH,
    --ag
    --
    Artie Gold -- Austin, Texas
     
    Artie Gold, Jul 19, 2003
    #1
    1. Advertising

  2. Artie Gold

    Artie Gold Guest

    Andrey Tarasevich wrote:
    > Artie Gold wrote:
    >
    >>...
    >>You'll need to define an `operator==' for the class A, as well as a
    >>functor that takes two A*s and compares the objects they point to -- and
    >>pass that functor as the second argument to the set template.
    >>...

    >
    >
    > Operator '==' is completely useless here, unless you are planning to
    > make it to perform a 'less than' type of comparison, more suited for
    > operator '<'. Operator '==' is a very strange choice of operator to
    > overload...
    >


    Yup. <smacks forehead>

    Mea culpa.

    --ag

    --
    Artie Gold -- Austin, Texas
     
    Artie Gold, Jul 19, 2003
    #2
    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. Ian Glover

    Re: Query on STL set container

    Ian Glover, Jul 20, 2003, in forum: C++
    Replies:
    0
    Views:
    2,591
    Ian Glover
    Jul 20, 2003
  2. Maitre Bart
    Replies:
    2
    Views:
    551
    Maitre Bart
    Feb 11, 2004
  3. Replies:
    4
    Views:
    832
    Daniel T.
    Feb 16, 2006
  4. wolverine
    Replies:
    2
    Views:
    485
    Marcus Kwok
    Jul 24, 2006
  5. puzzlecracker
    Replies:
    8
    Views:
    315
Loading...

Share This Page