<p>I was looking at the API documentation for stl vector, and noticed there was no method on the vector class that allowed the removal of an element with a certain value. This seems like a common operation, and it seems odd that there's no built in way to do this.</p>

[{'answer_id': 39923, 'author': 'bradtgmurray', 'author_id': 1546, 'author_profile': 'https://Stackoverflow.com/users/1546', 'pm_score': 4, 'selected': False, 'text': '<p>Use the global method std::remove with the begin and end iterator, and then use std::vector.erase to actually remove the elements.</p>\n\n<p>Documentation links<br>\nstd::remove <a href="http://www.cppreference.com/cppalgorithm/remove.html" rel="noreferrer">http://www.cppreference.com/cppalgorithm/remove.html</a><br>\nstd::vector.erase <a href="http://www.cppreference.com/cppvector/erase.html" rel="noreferrer">http://www.cppreference.com/cppvector/erase.html</a></p>\n\n<pre><code>std::vector&lt;int&gt; v;\nv.push_back(1);\nv.push_back(2);\n\n//Vector should contain the elements 1, 2\n\n//Find new end iterator\nstd::vector&lt;int&gt;::iterator newEnd = std::remove(v.begin(), v.end(), 1);\n\n//Erase the "removed" elements.\nv.erase(newEnd, v.end());\n\n//Vector should now only contain 2\n</code></pre>\n\n<p>Thanks to Jim Buck for pointing out my error.</p>\n'}, {'answer_id': 39941, 'author': 'Xavier Nodet', 'author_id': 4177, 'author_profile': 'https://Stackoverflow.com/users/4177', 'pm_score': 2, 'selected': False, 'text': '<p>See also <a href="https://en.cppreference.com/w/cpp/algorithm/remove" rel="nofollow noreferrer">std::remove_if</a> to be able to use a predicate...</p>\n\n<p>Here\'s the example from the link above:</p>\n\n<pre><code>vector&lt;int&gt; V;\nV.push_back(1);\nV.push_back(4);\nV.push_back(2);\nV.push_back(8);\nV.push_back(5);\nV.push_back(7);\n\ncopy(V.begin(), V.end(), ostream_iterator&lt;int&gt;(cout, " "));\n    // The output is "1 4 2 8 5 7"\n\nvector&lt;int&gt;::iterator new_end = \n    remove_if(V.begin(), V.end(), \n              compose1(bind2nd(equal_to&lt;int&gt;(), 0),\n                       bind2nd(modulus&lt;int&gt;(), 2)));\nV.erase(new_end, V.end()); [1]\n\ncopy(V.begin(), V.end(), ostream_iterator&lt;int&gt;(cout, " "));\n    // The output is "1 5 7".\n</code></pre>\n'}, {'answer_id': 39944, 'author': 'Jim Buck', 'author_id': 2666, 'author_profile': 'https://Stackoverflow.com/users/2666', 'pm_score': 9, 'selected': True, 'text': '<p><code>std::remove</code> does not actually erase elements from the container: it moves the elements to be removed to the end of the container, and returns the new end iterator which can be passed to <code>container_type::erase</code> to do the actual removal of the extra elements that are now at the end of the container:</p>\n<pre><code>std::vector&lt;int&gt; vec;\n// .. put in some values ..\nint int_to_remove = n;\nvec.erase(std::remove(vec.begin(), vec.end(), int_to_remove), vec.end());\n</code></pre>\n'}, {'answer_id': 39985, 'author': 'nsanders', 'author_id': 1244, 'author_profile': 'https://Stackoverflow.com/users/1244', 'pm_score': 3, 'selected': False, 'text': "<p>If you have an unsorted vector, then you can simply swap with the last vector element then <code>resize()</code>.</p>\n\n<p>With an ordered container, you'll be best off with \u200d<code>std::vector::erase()</code>.  Note that there is a <code>std::remove()</code> defined in <code>&lt;algorithm&gt;</code>, but that doesn't actually do the erasing.  (Read the documentation carefully).</p>\n"}, {'answer_id': 40008, 'author': 'Luke Halliwell', 'author_id': 3974, 'author_profile': 'https://Stackoverflow.com/users/3974', 'pm_score': 3, 'selected': False, 'text': "<p>The other answers cover how to do this well, but I thought I'd also point out that it's not really odd that this isn't in the vector API: it's inefficient, linear search through the vector for the value, followed by a bunch of copying to remove it.</p>\n\n<p>If you're doing this operation intensively, it can be worth considering std::set instead for this reason.</p>\n"}, {'answer_id': 15998752, 'author': 'Etherealone', 'author_id': 1576556, 'author_profile': 'https://Stackoverflow.com/users/1576556', 'pm_score': 6, 'selected': False, 'text': "<p>If you want to remove <strong><em>an</em></strong> item, the following will be a bit more efficient.</p>\n\n<pre><code>std::vector&lt;int&gt; v;\n\n\nauto it = std::find(v.begin(), v.end(), 5);\nif(it != v.end())\n    v.erase(it);\n</code></pre>\n\n<p>or you may avoid overhead of moving the items if the order does not matter to you:</p>\n\n<pre><code>std::vector&lt;int&gt; v;\n\nauto it = std::find(v.begin(), v.end(), 5);\n\nif (it != v.end()) {\n  using std::swap;\n\n  // swap the one to be removed with the last element\n  // and remove the item at the end of the container\n  // to prevent moving all items after '5' by one\n  swap(*it, v.back());\n  v.pop_back();\n}\n</code></pre>\n"}, {'answer_id': 45186119, 'author': 'jhasse', 'author_id': 647898, 'author_profile': 'https://Stackoverflow.com/users/647898', 'pm_score': 2, 'selected': False, 'text': '<p>A shorter solution (which doesn\'t force you to repeat the vector name 4 times) would be to use Boost:</p>\n\n<pre><code>#include &lt;boost/range/algorithm_ext/erase.hpp&gt;\n\n// ...\n\nboost::remove_erase(vec, int_to_remove);\n</code></pre>\n\n<p>See <a href="http://www.boost.org/doc/libs/1_64_0/libs/range/doc/html/range/reference/algorithms/new/remove_erase.html" rel="nofollow noreferrer">http://www.boost.org/doc/libs/1_64_0/libs/range/doc/html/range/reference/algorithms/new/remove_erase.html</a></p>\n'}, {'answer_id': 47603631, 'author': 'Katianie', 'author_id': 389832, 'author_profile': 'https://Stackoverflow.com/users/389832', 'pm_score': -1, 'selected': False, 'text': "<p>If you want to do it without any extra includes: </p>\n\n<pre><code>vector&lt;IComponent*&gt; myComponents; //assume it has items in it already.\nvoid RemoveComponent(IComponent* componentToRemove)\n{\n    IComponent* juggler;\n\n    if (componentToRemove != NULL)\n    {\n        for (int currComponentIndex = 0; currComponentIndex &lt; myComponents.size(); currComponentIndex++)\n        {\n            if (componentToRemove == myComponents[currComponentIndex])\n            {\n                //Since we don't care about order, swap with the last element, then delete it.\n                juggler = myComponents[currComponentIndex];\n                myComponents[currComponentIndex] = myComponents[myComponents.size() - 1];\n                myComponents[myComponents.size() - 1] = juggler;\n\n                //Remove it from memory and let the vector know too.\n                myComponents.pop_back();\n                delete juggler;\n            }\n        }\n    }\n}\n</code></pre>\n"}, {'answer_id': 52623923, 'author': 'DecPK', 'author_id': 9153448, 'author_profile': 'https://Stackoverflow.com/users/9153448', 'pm_score': 0, 'selected': False, 'text': "<p>Two ways are there by which you can use to erase an item particularly. \nlets take a vector</p>\n\n<pre><code>std :: vector &lt; int &gt; v;\nv.push_back(10);\nv.push_back(20);\nv.push_back(30);\nv.push_back(40);\nv.push_back(40);\nv.push_back(50);\n</code></pre>\n\n<p><strong>1) Non efficient way :</strong> Although it seems to be quite efficient but it's not because erase function delets the elements and shifts all the elements towards left by 1. \n<strong>so its complexity will be O(n^2)</strong></p>\n\n<pre><code>std :: vector &lt; int &gt; :: iterator itr = v.begin();\nint value = 40;\nwhile ( itr != v.end() )\n{\n   if(*itr == value)\n   { \n      v.erase(itr);\n   }\n   else\n       ++itr;\n}\n</code></pre>\n\n<p><strong>2) Efficient way ( RECOMMENDED )</strong> : It is also known as <strong><em>ERASE - REMOVE idioms</em></strong> .  </p>\n\n<ul>\n<li>std::remove transforms the given range into a range with all the elements that compare not equal to given element shifted to the start of the container.</li>\n<li>So, actually don't remove the matched elements.\nIt just shifted the non matched to starting and gives an iterator to new valid end.\nIt just requires O(n) complexity.</li>\n</ul>\n\n<p>output of the remove algorithm is : </p>\n\n<pre><code>10 20 30 50 40 50 \n</code></pre>\n\n<p>as return type of remove is iterator to the new end of that range.</p>\n\n<pre><code>template &lt;class ForwardIterator, class T&gt;\n  ForwardIterator remove (ForwardIterator first, ForwardIterator last, const T&amp; val);\n</code></pre>\n\n<p>Now use vector’s erase function to delete elements from the new end to old end of the vector. It requires O(1) time.</p>\n\n<pre><code>v.erase ( std :: remove (v.begin() , v.end() , element ) , v.end () );\n</code></pre>\n\n<p><strong>so this method work in O(n)</strong></p>\n"}, {'answer_id': 56157255, 'author': 'Pavan Chandaka', 'author_id': 6866309, 'author_profile': 'https://Stackoverflow.com/users/6866309', 'pm_score': 3, 'selected': False, 'text': '<p>From <strong>c++20</strong>:</p>\n\n<p>A non-member function introduced <code>std::erase</code>, which takes the vector and value to be removed as inputs.</p>\n\n<p>ex:</p>\n\n<pre><code>std::vector&lt;int&gt; v = {90,80,70,60,50};\nstd::erase(v,50);\n</code></pre>\n'}, {'answer_id': 61587400, 'author': 'Harshad Sharma', 'author_id': 9060084, 'author_profile': 'https://Stackoverflow.com/users/9060084', 'pm_score': 2, 'selected': False, 'text': '<p>*</p>\n\n<blockquote>\n  <p>C++ community has heard your request :)</p>\n</blockquote>\n\n<p>*</p>\n\n<p><strong>C++ 20</strong> provides an easy way of doing it now. \nIt gets as simple as :</p>\n\n<pre><code>#include &lt;vector&gt;\n...\nvector&lt;int&gt; cnt{5, 0, 2, 8, 0, 7};\nstd::erase(cnt, 0);\n</code></pre>\n\n<p>You should check out <a href="https://en.cppreference.com/w/cpp/container/vector/erase2" rel="nofollow noreferrer">std::erase</a> and <a href="https://en.cppreference.com/w/cpp/container/vector/erase2" rel="nofollow noreferrer">std::erase_if</a>.</p>\n\n<p>Not only will it remove all elements of the value (here \'0\'), it will do it in <strong><em>O(n)</em></strong> time complexity. Which is the very best you can get.</p>\n\n<p>If your compiler does not support C++ 20, you should use <a href="https://en.wikipedia.org/wiki/Erase%E2%80%93remove_idiom" rel="nofollow noreferrer">erase-remove idiom</a>:</p>\n\n<pre><code>#include &lt;algorithm&gt;\n...\nvec.erase(std::remove(vec.begin(), vec.end(), 0), vec.end());\n</code></pre>\n'}, {'answer_id': 72653247, 'author': 'Antonio', 'author_id': 2436175, 'author_profile': 'https://Stackoverflow.com/users/2436175', 'pm_score': 0, 'selected': False, 'text': '<p>Similar to the <a href="https://en.wikipedia.org/wiki/Erase%E2%80%93remove_idiom" rel="nofollow noreferrer">erase remove idiom</a>, for <code>vector</code> one could use <code>resize</code> and <code>remove</code> and use iterator distance computation:</p>\n<pre><code>std::vector&lt;int&gt; vec;\n// .. put in some values ..\nint int_to_remove = n;\nvec.resize(std::remove(vec.begin(), vec.end(), int_to_remove) - vec.begin());\n</code></pre>\n<p>Tested <a href="https://coliru.stacked-crooked.com/a/a54995654e909f79" rel="nofollow noreferrer">here</a>.</p>\n'}]
['https://Stackoverflow.com/questions/39912', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1546/']