Created
June 24, 2017 14:24
-
-
Save derofim/aa2e335a0346895b9a9bf406ce02330d to your computer and use it in GitHub Desktop.
C++ vector erase reverse iterator base
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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