Skip to content

Instantly share code, notes, and snippets.

@krysseltillada
Created July 25, 2015 11:49
Show Gist options
  • Save krysseltillada/3ebc6128dc902a6b955a to your computer and use it in GitHub Desktop.
Save krysseltillada/3ebc6128dc902a6b955a to your computer and use it in GitHub Desktop.
std::erase
#include <iostream>
#include <list>
#include <vector>
int main ()
{
int ia[11] = {0, 1, 1, 2, 3, 5, 8, 13, 21, 55, 89}; /// declare and define ia as int array that has a size of 11 (built in array)
std::list <int> lst (std::begin(ia), std::end(ia)); /// initialize a value range from one past to the end of the array
std::vector <int> v1 (std::begin(ia), std::end(ia)); /// same
auto it1 = lst.begin (); /// stores the iterator from lst
auto it2 = v1.begin (); /// stores the iterator from v1
while (it1 != lst.end()) { /// deletes odd values
if (*it1 % 2)
it1 = lst.erase (it1);
else
++it1;
}
while (it2 != v1.end()) { /// deletes even values
if (*it2 % 2)
++it2;
else
it2 = v1.erase (it2);
}
std::cout << std::endl;
std::cout << "lst:" << std::endl;
for (auto i : lst) /// displays the elements in lst
std::cout << i << " ";
std::cout << std::endl;
std::cout << "v1" << std::endl;
for (auto ii : v1) /// displays the elements in v1
std::cout << ii << " ";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment