How can I find an element in a set which contains pointers to the elements?

set::find c++
c set::find complexity
find pair in set c++
c++ set of pointers

Edit: I fixed my mistake: I'm using a set and not a vector.

Please consider the following example code:

set<Foo *> set_of_foos;

set_of_foos.insert(new Foo(new Bar("x")));
set_of_foos.insert(new Foo(new Bar("y")));
[...]

// The way a "foo" is found is not important for the example.
bool find_foo(Foo *foo) {
  return set_of_foos.end() != set_of_foos.find(foo);
}

Now when I call:

find_foo(new Foo(new Bar("x")));

the function returns false since what I'm looking for can't be found. The reason is obvious to me: The pointers point to different objects since they are allocated both with a new, resulting in different values of the addresses.

But I want to compare the contents of Foo (i.e. "x" in the above example) and not Foo * itself. Using Boost is not an option as well as modifying Foo.

Do I need to loop through each of the Foo * inside set_of_foos or is there a simpler solution? I tried uniquely serializing the contents of each Foo and replace the set<Foo *> with a map<string, Foo *>, but this seems like a very "hacked" solution and not very efficient.

find_foo(new Foo(new Bar("x"))); does not sound like a good idea - it will most likely (in any scenario) lead to memory leak with that search function.

You could use find_if with a functor:

struct comparator {
    Foo* local;
    comparator(Foo* local_): local(local_) {}
    ~comparator() { /* do delete if needed */ } 
    bool operator()(const Foo* other) { /* compare local with other */ }
};

bool found = vec.end() != std::find_if(vec.begin(), vec.end(), comparator(new Foo(...)));

Conference Record of the Eighteenth Annual ACM Symposium on , If variable x contains a pointer to variable y, expression x7 retrieves the value stored Consider a multiset M of pointers to elements in a set S. For each element  Syntax: boolean contains (Object element) Parameters: The parameter element is of the type of Set. This is the element that needs to be tested if it is present in the set or not. Return Value: The method returns true if the element is present in the set else return False. Below program illustrate the Java.util.Set.contains () method: filter_none.

Change your vector to set with your custom comparable function to compare Foo objects.

Should be:

struct ltFoo
{
  bool operator()(Foo* f, Foo* s) const
  {
    return f->value() < s->value();
  }
};

set<Foo*, ltFoo> sFoo;
sFoo.insert(new Foo(new Bar("x"));
sFoo.insert(new Foo(new Bar("y"));

if (sFoo.find(new Foo(new Bar("y")) != sFoo.end())
{
    //exists
}
else
{
    //not exists
}

Data Structures: Theory and Practice, As in the 2-word element, one of the words contains pointers, but now a single One possibility is to use elements whose size may be one of a restricted set of  The set::find is a built-in function in C++ STL which returns an iterator to the element which is searched in the set container. If the element is not found, then the iterator points to the position just after the last element in the set. Syntax: set_name.find(element) Parameters: The function accepts one mandatory parameter element which

Do I need to loop through each of the Foo * inside vector_of_foos or is there a simpler solution?

You do need to loop to find what you want, but you can use std::find_if or another "wrapped loop". This is more natural with lambdas in C++0x, but in C++03 I'd just use a regular for loop, possibly wrapped in your own function if you need to do this in more than one place.

Proceedings of the Twelfth Annual ACM-SIAM Symposium on Discrete , It is tagged if the set contains at least one element. supernode is tagged it contains pointers to the leaves representing the minimum and marimum elements. Search for an element in Set using set::find() std::set provides a member function to find the existance of a given element in set i.e. iterator find (const value_type& val); It accepts the element as argument and search for that in the set. If the element is found in set then it returns the iterator pointing to that element else it returns the iterator pointing to the end of set.

Instead of using std::find, use std::find_if and provide your own predicate. This of course relies in you being able to access the member that holds "x" in Foo.

struct FooBar
{
  FooBar(Foo* search) : _search(search){}
  bool operator(const Foo* ptr)
  {
    return ptr->{access to member} == _search->{access to member};
  }

  Foo* _search;
}

vector<Foo*>::iterator it = std::find_if(vec.begin(), vec.end(), FooBar(new Foo(new Bar("x")));

If you can't access the member and you can guarantee that all other members will be the same, you could try a bare memcmp in the above functor rather than "==".

Fundamentals of Parameterized Complexity, JUMP NUMBER Instance: A finite ordered set P, and a positive integer k. Parameter: k. The inlist of an element a contains pointers to elements covered by a. Different ways to Erase / Delete an element from a Set in C++; Python : Check if a String contains a sub string & find it's index | case insensitive; C++ : How to copy / clone a STL List or Sub List; Python : How to find an element in Tuple by value; Python: Find index of element in List (First, last or all occurrences)

You may consider also using the Boost Ptr container library. It allows having a list of pointers using standard algorithms, find, etc. as if it contained objects, and automatically releasing the memory used by the pointers upon vector deletion.

C++ : How to check if a Set contains an element, As std::set can contain only unique elements, so occurrence count of the given element can be only 0 or 1 i.e,. If found then 1 else 0. Let's use set::  (c) Set A contains 5 elements and the set B contains 6 elements. For one-one function each element in set B is assigned to only one element in set A. Thus only '5' elements in set B are assigned to '5' elements of set 'A' Thus range of function does not contain all '6' elements of set ' B' . Thus if function is one-one it cannot be onto

Insertion and Deletion in STL Set C++, The use of hint pointer is to help insert() know where the actual insertion has to The element was newly inserted The set elements after 1st insertion are : 20  If all elements of input array are same, every element is a peak element. Recommended: Please solve it on “ PRACTICE ” first, before moving on to the solution. It is clear from the above examples that there is always a peak element in the input array.

[PDF] Why you shouldn't use set (and what you should use instead) Matt , A set may not contain more than one element with the same key, so insert won't As with all STL containers, you can step through all of the elements in a set using words of storage (a color marker, and pointers to two children and a parent). After this, a for loop is used to dereference the pointer and print all the elements in the array. The pointer is incremented in each iteration of the loop i.e at each loop iteration, the pointer points to the next element of the array. Then that array value is printed. This can be seen in the following code snippet.

set - C++ Reference, The value of the elements in a set cannot be modified once in the container This can be a function pointer or a function object (see constructor for an example​). A doubly linked list contains two pointers for each node, which means one pointer refers to the next node, and the other pointer refers to the previous node. Doubly linked lists can be traversed forward and backward and it is easier to insert anywhere in a list. It is easier to delete nodes in a

Comments
  • Point of detail: std::vector doesn't have a find() member function; did you mean std::find(vector_of_foos.begin(), vector_of_foos.end(), s)?
  • std::vector doesn't have a find member. Are you really using a different container type, or the global function std::find()?
  • changed it in the text, thanks for seeing that. I have a set, looked at the wrong line.
  • It depends how carefully the OP is about calling delete at the relevant times. As long as he iterates through vector_of_foos and invokes delete on each element at some point, and so long as ~Foo() invokes delete on whichever member-variable pointer, everything's ok.
  • Memory leaks and alike is not my real concern. This is just me playing around with C++. This line would be the last of the code so it wouldn't matter. But you're right, in a non-practice example is would be more carefull with delete.
  • @Oli: The OP isn't adding the parameter of find_foo to the vector and this is indeed a leak in any reasonable interpretation of the (pseudo-)code given; it sounds like the example has been generalized too much.
  • @Roger: Ah, yes, I didn't see the find_foo()... I absolutely agree, in that case! However, this isn't an answer to the question, though.
  • The Foo here is definitely leaked. The created pointer is passed only to find_foo(). The definition of find_foo is given, and it does not delete its argument. This looks like the Java idiom, and I suspect the poster needs to learn to use new in C++ only when necessary.
  • This is one answer. Note that it changes the set to sort by a user-defined order instead of by address. There's an implicit assumption that there is never a NULL pointer in or passed to sFoo. And the Foo in the call to find() should not be newed. (Hard to say about the Bar without more info.)
  • This is an interessting approach. The problem is, that there is a dynamic size of how many elements Foo contains e.g. 2 Bar, 4 Bar, ... and I'm not sure, how to get the right match for multiple elements.
  • @DrColossos - -1 for a terribly-written question - your code and stated assumptions change by the minute. Nowhere but here do you mention Foo containing multiple Bars. How can people possibly give sensible answers?
  • You probably don't want your key element to be created on the heap, it's not getting deleted. I updated the example to use a key object on the stack.