#include <list>
#include <vector>
#include <algorithm>
#include <iostream>
#include <stdexcept>
using namespace std;

void assert(bool result)
{
	if (!result)
		throw std::runtime_error("assert failed");
}

void removeOdd(list<int>& li)
{
    for(list<int>::iterator it = li.begin(); it != li.end(); )
    {
		//cout << "checking " << *it << endl;
        if ((*it) % 2)
        {
            cout << "erasing " << *it << endl;
            it = li.erase(it);
        } 
        else
        {
        	cout << "keeping " << *it << endl;
        	++it;
        }
    }
}

void test()
{
    int a[9] = { 5, 2, 8, 9, 6, 7, 3, 4, 1 };

    list<int> x(a, a+9);  // construct x from the array
    assert(x.size() == 9 && x.front() == 5 && x.back() == 1);

    cout << "before remove: [ ";
    for(int val : x) {
    	cout << val << ' ';
    }
    cout << ']' << endl;

    removeOdd(x);
    assert(x.size() == 4);

    cout << "after remove: [ ";
    for(int val : x) {
    	cout << val << ' ';
    }
    cout << ']' << endl;

    vector<int> v(x.begin(), x.end());  // construct v from x
    sort(v.begin(), v.end());
    cout << "after sort: [ ";
    for(int val : v) {
    	cout << val << ' ';
    }
    cout << ']' << endl;

    int expect[4] = { 2, 4, 6, 8 };
    for (int k = 0; k < 4; k++){
        assert(v[k] == expect[k]);
    }
}

int main()
{
    test();
    cout << "Passed" << endl;
}