#Pointer comparisons

24 messages · Page 1 of 1 (latest)

opal trench
#
bool vectorContains(std::vector<DataType>& vec, DataType& data) { 
  return (&data >= &*vec.begin()) && (&data < &*vec.end());
}

if i have an element by reference, theoretically, one could check for its existence inside the vector like this, right?

wheat mountainBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

opal trench
#

@topaz hatch we can discuss here so that the text channel does not get cluttered

topaz hatch
#

But yeah, basically, the standard only defines pointer comparison for different pointers when:

  • Two pointers in the same array
  • Two pointers to different data members (non static) of the same object, subobjects of that object (recursively) when the subobjectis not zero sized or a union
#

Any other comparisons are not defined. So, your vector contains is only well defined if it indeed contains the object

opal trench
#

right

#

so my original problem was that, i have a pointer to an object i am sure is contained in one of two vectors, and i want to determine which vector the object is in because i want to remove the object from the corresponding vector. is there a way to do this without resorting to linear search?

topaz hatch
#

Is the data sorted?

opal trench
#

no

topaz hatch
#

Then linear it is

opal trench
topaz hatch
#

You could also use a bloom filter as an early out, but that's only a test for does not contain, it doesn't prove that it contains

opal trench
#

i see

topaz hatch
#

If you need a cheap lookup, you can use a hash set too, instead of a vector

#

But if your data set is small, linear search is cheap

opal trench
#

i see

#

i am going to be iterating through this container a lot so i want to use a vector

#

well i found another fix for my problem in context what i am doing

opal trench
#

pointer1 + pointer2

topaz hatch
opal trench
#

indeed

#

!solved