Skip to content

Instantly share code, notes, and snippets.

@utilForever
Created February 9, 2017 09:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save utilForever/8dd7adb233543f91493dcc02f2600811 to your computer and use it in GitHub Desktop.
Save utilForever/8dd7adb233543f91493dcc02f2600811 to your computer and use it in GitHub Desktop.
Erase element in reverse_iterator (reverse_iterator unchanged method, and advanced method)
#include <iostream>
#include <vector>
int main(int argc, char* argv[])
{
std::vector<int> v = {1, 2, 3, 4, 5};
for (auto iter = v.rbegin(); iter != v.rend(); ++iter)
{
std::cout << *iter << '\n';
if (*iter == 3)
{
v.erase(std::next(iter).base());
}
}
std::cout << '\n';
for (auto val : v)
{
std::cout << val << '\n';
}
std::cout << '\n';
std::vector<int> v2 = {1, 2, 3, 4, 5};
for (auto iter = v2.rbegin(); iter != v2.rend(); ++iter)
{
std::cout << *iter << '\n';
if (*iter == 3)
{
std::advance(iter, 1);
v2.erase(iter.base());
}
}
std::cout << '\n';
for (auto val : v2)
{
std::cout << val << '\n';
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment