/*****************************************************************************
 * This is an illustration of list erasure. When used with a top value of
 * 100,000 and a divisor of 2, there is no obvious pause while the computer  
 * erases half the values in the starting vector. You may notice that the 
 * original list loads more slowly than a vector. 
 * ****************************************************************************/
#include <iostream>
#include<cctype>
#include <list>
#include<string>
using std::cin; using std::cout;            
using std::endl;  using std::string;          
using std::list;


void show_list(const list<int> & l);
int main()
{
	
	list<int> L, div;
	int num, top;
	cout<<"Please enter the top positive integer under consideration: ";
	cin>>top;
	for(int i=1; i!=top+1; i++)
		L.push_back(i);//Load the vector with values 1-top

	cout<<"\nEnter an integer. The program will partition the values "
	       <<"into values\nthat integer divides evenly and "
      	       <<"values it does not.\nYour integer :";
	cin>>num;
list<int>::iterator iter=L.begin();//This cannot be a const iterator
					     // because we plan to use it to 
					     // modify  V.
	while(iter!=L.end())
	{
		if(*iter%num==0)
		{
			div.push_back(*iter);//Push the value into the divisor
					    //vector.
			iter=L.erase(iter);//Erase it from the original. The
				           //erase function returns an iterator
					   //indicating the next value. 
					     
		}
		else
			++iter;
	}
	string ans;
	cout<<endl<<"Would you like a report? (y or n): ";
	cin>>ans;
	if(tolower(ans[0])=='y')
	{
	cout<<"\nMultiples of "<<num<<" are\n";
	show_list(div);
	cout<<endl<<"\nNon-multiples of "<<num<<" are\n";
	show_list(L);
	}
	return 0;
			
}
void show_list(const list<int>& l)
{
	for(list<int>::const_iterator iter=l.begin(); iter!=l.end();++iter)
		cout<<*iter<<" ";
	/* We need a const iterator here because we passed l by constant 
	 * reference.
	 */   
	return;
}
	
