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

using sample = std::pair<unsigned, double>;

std::ostream& operator<<(std::ostream& ostr, const std::list<sample>& list)
{
	for (auto &i : list) {
		ostr << "<" << i.first << "," << i.second << "> ";	}
	return ostr;
}

int main() {
	
	sample a{ 1,12.2 };
	sample b{ 2,11.778 };
	sample c{ 3,9.2 };
	sample d{ 4,-2.6 };
	sample e{ 5,10.1 };

	std::list<sample> samples{ d,c,b,a };
	cout << "list is: " << samples << endl << endl;

	double maxval = -std::numeric_limits<double>::infinity();
	unsigned cutoff = 2;
	samples.remove_if([&maxval,cutoff](sample s) 
	{
        //if older than cutoff, remove
        if (s.first < cutoff) return true;
        //otherwise, keep and update max
        maxval = std::max(maxval, s.second);
        return false;
    });
	cout << "max is: " << maxval << ", list is: " << samples << endl << endl;

	samples.push_front(e);
	maxval = -std::numeric_limits<double>::infinity();
	samples.remove_if([&maxval, cutoff](sample s)
	{
		if (s.first < cutoff) return true;
		maxval = std::max(maxval, s.second);
		return false;
	});
	cout << "max is: " << maxval << ", list is: " << samples << endl << endl;

	maxval = -std::numeric_limits<double>::infinity();
	cutoff = 3;
	samples.remove_if([&maxval,cutoff](sample s) 
	{
		if (s.first < cutoff) return true;
		maxval = std::max(maxval, s.second);
		return false;
	});
	cout << "max is: " << maxval << ", list is: " << samples << endl << endl;

	return 0;
}