removing element from vector while iterating c 2b 2b

Solutions on MaxInterview for removing element from vector while iterating c 2b 2b by the best coders in the world

showing results for - "removing element from vector while iterating c 2b 2b"
David
24 Jul 2019
1#include <iostream>
2#include <vector>
3#include <algorithm>
4 
5int main()
6{
7    std::vector<int> v = { 1, 2, 3, 4, 5, 6 };
8 
9    auto it = v.begin();
10    while (it != v.end())
11    {
12        // specify condition for removing element; in this case remove odd numbers
13        if (*it & 1) {
14            // erase() invalidates the iterator, use returned iterator
15            it = v.erase(it);
16        }
17        // Notice that iterator is incremented only on the else part (why?)
18        else {
19            ++it;
20        }
21    }
22 
23    for (int const &i: v) {
24        std::cout << i << ' ';
25    }
26 
27    return 0;
28}
29
queries leading to this page
c 2b 2b vector delete element while iteratingremove vector element c 2b 2b with iteratorc 2b 2b delete from vector inside loopcpp vector remove element by iteratorc 2b 2b vector remove element while iteratingc 2b 2b remove item from vector while iteratingc 2b 2b remove item from vector during iterationhow to remove an object from a vector c 2b 2b by iteratordeleting element of vector with an iteratorc 2b 2b vector remove iteratorc 2b 2b remove vector element in a vector loopc 2b 2b is removing an element from a vector while in a loop dangerouserase element from vector c 2b 2b in loopc 2b 2b iterate vector and remove elementsdeleting element in a vector cpp with itarator erasing an element from a vector c 2b 2b while remove element from vector c 2b 2b iteratorloop through vector while erasing elements c 2b 2bc 2b 2b loop through vector and deletec 2b 2b remove element from vector in for loopc 2b 2b iterator vector delete elementsc 2b 2b removing an object from a vector in a for loopc 2b 2b vector erase in loopc 2b 2b remove elements from vector while iteratingremove element from vector using iterator c 2b 2bc 2b 2b loop through vector and remove elementsc 2b 2b for loop remove item from vectorremove nth element from vector c 2b 2bremoving elements witha n interator c 2b 2bvector delete element by iteratorc 2b 2b remove from vector with iteratorhow to erase vector element in loopc 2b 2b remove element from vector while for loopremove from vector while iterating for loop c 2b 2bcpp iterate and remove from vectorerase from vector in a loopremove from vector while iterating c 2b 2bhow to remove an element from vector c 2b 2b by iteratorerase vector c 2b 2b while iteratinghow to erase some elemnts in vector in loophow to remove from vector using iterator c 2b 2bc 2b 2b vector remove while iteratingc 2b 2b erase vector element in for loopc 2b 2b remove element from vector using iteratorremove elements from vector in for loop c 2b 2bremove element from vector while iterating c 2b 2berase element from vector while iterating c 2b 2bdelete an element from vector c 2b 2b using iteratorhow to erase from vector c 2b 2b in looperase element in set in loopremove item from vector by iterator c 2b 2bremove vector element at current interatorerase vector from loop c 2b 2bdelete iterator from vectordelete from vector while iterating c 2b 2bhow to delete an item from vector by iterator c 2b 2bhow to delete element from a vector in c 2b 2b while iteratingdelete element from vector in for loopc 2b 2b remove element from vector while iteratingremove element from vector c 2b 2b in loopc 2b 2b remove element from vector in loopvector remove element by iteratorc 2b 2b for iteration remove c 2b 2b vector remove current element loopvector delete element while iteratingc 2b 2b iterator erase vector itemc 2b 2b vector erase current elementc 2b 2b delete element from vector while iteratingc 2b 2b erase element from vector while iteratingc 2b 2b erase from vector while iteratingremoving element from vector while iterating c 2b 2bc 2b 2b vector iterate and eraseerase from vector c 2b 2b while iteratingremoving element from vector while iterating c 2b 2b