-1

I have a set of pointers to a custom class myClass:

set<myClass*> s;

I'm trying to use std::find to verify whether an object O of type myClass * is contained within the set, but this doesn't appear to be working- I think find might only work for objects and not pointers to objects? Is there any way to make this work? I tried dereferencing the pointer but then I realized that obviously wouldn't work since my set is one of pointers to objects anyway.

set<myClass*> s;
/* set gets filled up with objects of type myClass */
myClass *O = new myClass();
if(s.find(O) != s.end())
    cout<<"Found!\n";

I've manually printed out the members of O and an object already in the set, and I know they're identical. So this code should be printing "Found!" But it's not.

Roshan Krishnan
  • 187
  • 1
  • 3
  • 13

2 Answers2

2

You have a set of pointers, and you don't add your pointer in the set, so it is not found.

Following will found your pointer:

std::set<myClass*> s;
/* set gets filled up with objects of type myClass */
myClass *O = new myClass();
s.insert(O); // O added in set.
if (s.find(O) != s.end())
    cout<<"Found!\n"; // will be found

Else you may use a custom comparer.

Assuming you have bool operator < (const myClass& lhs, const myClass& rhs)

you may do

struct myClassComp {
    bool operator() (const myClass* lhs, const myClass* rhs) const
    {
        return *lhs < *rhs;
    }
};

and then

std::set<myClass*, myClassComp> s;
myClass O1;
s.insert(&O1);
myClass O2;

if (s.find(O2) != s.end())
    std::cout << "Found!\n"; // will be found
Jarod42
  • 203,559
  • 14
  • 181
  • 302
  • I am inserting my objects into the set, I just didn't include those lines of code because they are located elsewhere. That first example is exactly what I'm doing, and it does not work. I've tried both creating a custom comparator and overriding the == operator to make sure my class comparisons are correct, and neither helps. – Roshan Krishnan Sep 16 '15 at 02:19
  • In your snippet, you cannot have add `O` (pointer), you may have add an other pointer on a default `myClass`. Anyway, you compare pointer, not value. – Jarod42 Sep 16 '15 at 07:28
0

A set of pointer is organized by address of inserted objects, not the contents of the object. So looking in the set for O is looking in the set for an object with the address O points at. Since computers typically do not allow two objects to be at the same memory location, you will never find O.

For example you have strings O and P. O and P are initialized to the same value, "Hello world!" but are different objects and have different addresses.

You have set<string*> myset; and you insert P into the set myset.insert(&P);. So far everything is groovy.

But when you try and find O in myset you can't because the set is not trying to find "Hello World!", it is looking for &O and &O is not &P.

So you need to compare O and P, not &O and &P.

And at this point, I direct your attention here: Using custom std::set comparator

Community
  • 1
  • 1
user4581301
  • 33,082
  • 7
  • 33
  • 54