0

I want to delete a unique_pointer from a std::vector.

class Obj
{
   std::string name;
   std::string getName();
}

Now i have a vector of unique pointers of the class obj Obj

std::vector<std::unique_ptr<Obj>> objects;
for( int i = 0; i < objects.size(); i++)
{
    if( objects[i]->getName() == "NameOftheObject" )
      objects.erase( objects.begin() + i );
}

Now instead of running a loop can we do this through standard algorithm ?

Summit
  • 2,112
  • 2
  • 12
  • 36
  • 2
    use the remove/erase idiom. See the examples on the bottom of this page. https://en.cppreference.com/w/cpp/algorithm/remove – JohnFilleau Mar 02 '20 at 03:45
  • Does this answer your question? [std::vector removing elements which fulfill some conditions](https://stackoverflow.com/questions/17270837/stdvector-removing-elements-which-fulfill-some-conditions) and [How to remove an element from std::vector if the element id matches a search parameter](https://stackoverflow.com/questions/33108332/how-to-remove-an-element-from-stdvector-if-the-element-id-matches-a-search-par) – walnut Mar 02 '20 at 03:50

0 Answers0