Skip to content

Instantly share code, notes, and snippets.

@derofim
Created June 24, 2017 14:24
Show Gist options
  • Save derofim/aa2e335a0346895b9a9bf406ce02330d to your computer and use it in GitHub Desktop.
Save derofim/aa2e335a0346895b9a9bf406ce02330d to your computer and use it in GitHub Desktop.
C++ vector erase reverse iterator base
// Example program
#include <iostream>
#include <string>
#include <vector>
int main()
{
std::vector<int> vec{0,1,2,3,4,5,6,7,8,9,10,11,12,13};
for(auto it = vec.rbegin();it!=vec.rend();it++)
{
if((*it)%2==0) {
//std::cout<< * it << std::endl;
// The base iterator refers to the element that is next to the element the reverse_iterator is currently pointing to.
vec.erase(it.base()-1);
}
}
/*for(auto it = vec.begin();it!=vec.end();)
{
if(*it%2==0) {
//std::cout<< * it << std::endl;
it = vec.erase(it);
} else {
++it;
}
}*/
// 97531
for(auto it = vec.rbegin();it!=vec.rend();it++)
{
std::cout<< * it << std::endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment