Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding an element in a shared_ptr container?

I'm having a little problem with finding an element in a vector of shared_ptr.

Here is what I ended up with:

std::vector<std::shared_ptr<Block>> blocks;

bool contains(Block* block) {
  for (auto i = blocks.begin(); i != blocks.end(); ++i) {
    if ((*i).get() == block) {
      return true;
    }
  }
  return false;
}

However, I didn't managed to do it with std::find or even std::find_if. Is there a more c++ compliant way to achieve this ?

EDIT: This is the code I have after the answer:

bool contains(Block* block) {
  auto found = std::find_if(blocks.begin(), blocks.end(), [block](std::shared_ptr<Block> const& i){
    return i.get() == block;
  });
  return found != blocks.end();
}
like image 897
bquenin Avatar asked Feb 17 '13 01:02

bquenin


People also ask

What does shared_ptr get () do?

A shared_ptr may share ownership of an object while storing a pointer to another object. get() returns the stored pointer, not the managed pointer.

Why would you choose shared_ptr instead of Unique_ptr?

In short: Use unique_ptr when you want a single pointer to an object that will be reclaimed when that single pointer is destroyed. Use shared_ptr when you want multiple pointers to the same resource.

Can you return a shared_ptr?

So the best way to return a shared_ptr is to simply return by value: shared_ptr<T> Foo() { return shared_ptr<T>(/* acquire something */); }; This is a dead-obvious RVO opportunity for modern C++ compilers.

How many bytes is a shared_ptr?

In a typical implementation, std::shared_ptr holds only two pointers. So 1000 shared pointers take up 1000 * 2 * sizeof(pointer) bytes of memory. Size of a pointer is 4 bytes on all 32-bit systems that follow ILP32 data model.


2 Answers

Try:

std::find_if(blocks.begin(), blocks.end(), 
  [block](std::shared_ptr<Block> const& i){ return i.get() == block; });
like image 58
user1095108 Avatar answered Oct 19 '22 05:10

user1095108


Even simpler:

bool contains(Block* block) {
  return std::any_of(blocks.cbegin(), blocks.cend(),
                     [block](std::shared_ptr<Block> const& i) { return i.get() == block; });
}
like image 34
Daniel Laügt Avatar answered Oct 19 '22 05:10

Daniel Laügt