Skip to content

Instantly share code, notes, and snippets.

@jacquelineIO
Last active December 16, 2015 12:39
Show Gist options
  • Save jacquelineIO/5436361 to your computer and use it in GitHub Desktop.
Save jacquelineIO/5436361 to your computer and use it in GitHub Desktop.
#include <vector>
#include <iostream>
#include <stdio.h>
int main (int argv, char ** argc)
{
std::vector<int> v;
std::vector<int>::iterator vIter;
std::vector<int>::reverse_iterator vRvIter;
for (int i = 1; i <= 5; i++)
{
v.push_back(i);
}
// -------------------------------------------------------------------
std::cout << "Print with iteraor 1-5 remove 3 and 5" << std::endl;
vIter = v.begin();
for ( ; vIter != v.end(); vIter++ )
{
std::cout << *vIter << " ";
if (*vIter == 3)
{
vIter = v.erase(vIter);
vIter--;
}
if (*vIter == 5)
{
vIter = v.erase(vIter);
vIter--;
}
}
std::cout << std::endl;
// Print again
vIter = v.begin();
for ( ; vIter != v.end(); vIter++ )
{
std::cout << *vIter << " ";
}
std::cout << std::endl;
// -------------------------------------------------------------------
std::cout << "Print with resverse_iteraor 1,2,4, remove 4" << std::endl;
vRvIter = v.rbegin();
for ( ; vRvIter != v.rend(); )
{
std::cout << *vRvIter << " ";
if ( *vRvIter == 4)
{
vRvIter++;
vRvIter = std::vector<int>::reverse_iterator(
v.erase(vRvIter.base()) );
}
else
{
vRvIter++;
}
}
std::cout << std::endl;
// Print again
vRvIter = v.rbegin();
for ( ; vRvIter != v.rend(); vRvIter++ )
{
std::cout << *vRvIter << " ";
}
std::cout << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment