Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: Query on STL set container

Reply
Thread Tools

Re: Query on STL set container

 
 
Artie Gold
Guest
Posts: n/a
 
      07-19-2003
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

 
Reply With Quote
 
 
 
 
Artie Gold
Guest
Posts: n/a
 
      07-19-2003
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

 
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
container within a container issue: set in the map puzzlecracker C++ 8 09-21-2008 11:08 PM
container inside container in stl wolverine C++ 2 07-24-2006 03:08 PM
Copy elements from one STL container to another STL container Marko.Cain.23@gmail.com C++ 4 02-16-2006 05:03 PM
STL: container's values setup by another container Maitre Bart C++ 2 02-11-2004 12:11 AM
Re: Query on STL set container Ian Glover C++ 0 07-20-2003 02:56 AM



Advertisments