#include <iostream>
#include <algorithm>
#include <vector>
int main()
{
std::vector<int> container = {1,2,3,4,3,2,1,2,3,4};
// output
std::cout << "Before: ";
for(int n: container) std::cout << n << ' ';
std::cout << '\n';
// erase every 2 using erase-remove
int temp = 2;
container.erase( std::remove(container.begin(), container.end(), temp),
container.end() );
// output
std::cout << "After erasing 2: ";
for(int n: container) std::cout << n << ' ';
std::cout << '\n';
// same thing (but slower) with a loop
temp = 3;
std::vector<int>::iterator itr1, itr2 = container.end();
for (itr1=container.begin(); itr1 != itr2; )
{
if(*itr1==temp)
itr1 = container.erase(itr1);
else
++itr1;
}
// output
std::cout << "After erasing 3: ";
for(int n: container) std::cout << n << ' ';
std::cout << '\n';
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8YWxnb3JpdGhtPgojaW5jbHVkZSA8dmVjdG9yPgoKaW50IG1haW4oKQp7CiAgICBzdGQ6OnZlY3RvcjxpbnQ+IGNvbnRhaW5lciA9IHsxLDIsMyw0LDMsMiwxLDIsMyw0fTsKCiAgICAvLyBvdXRwdXQKICAgIHN0ZDo6Y291dCA8PCAiQmVmb3JlOiAiOwogICAgZm9yKGludCBuOiBjb250YWluZXIpIHN0ZDo6Y291dCA8PCBuIDw8ICcgJzsKICAgIHN0ZDo6Y291dCA8PCAnXG4nOwogICAgCiAgICAvLyBlcmFzZSBldmVyeSAyIHVzaW5nIGVyYXNlLXJlbW92ZQogICAgaW50IHRlbXAgPSAyOwogICAgY29udGFpbmVyLmVyYXNlKCBzdGQ6OnJlbW92ZShjb250YWluZXIuYmVnaW4oKSwgY29udGFpbmVyLmVuZCgpLCB0ZW1wKSwKICAgICAgICAgICAgICAgICAgICAgY29udGFpbmVyLmVuZCgpICk7CiAgICAKICAgIC8vIG91dHB1dAogICAgc3RkOjpjb3V0IDw8ICJBZnRlciBlcmFzaW5nIDI6ICI7CiAgICBmb3IoaW50IG46IGNvbnRhaW5lcikgc3RkOjpjb3V0IDw8IG4gPDwgJyAnOwogICAg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