C++ primer plus第六版课后编程题答案18.5


 

 

第十八章答案均由热心博友:王琪胜 提供

邮箱:wqs1639@gmail.com

如果对答案有疑问的可以发Email给我或者该博友,感谢大家一直以来的支持,

祝大家都能在编程中找到自己的最亮的那个闪光点。

————————————————————————————————————————————————————

18.5

//functor.cpp--using a-functor
#include <iostream>
#include <list>
#include <iterator>
#include <algorithm>
template<class T>
class TooBig{
private:
	T cutoff;
public:
	TooBig(const T&t):cutoff(t){};
	bool operator()(const T&v){return v>cutoff;}
};

void outint(int n){std::cout<<n<<" ";}
int main(){
	using namespace std;
	TooBig<int> f100(100);
	int vals[10]={50,100,90,180,60,210,415,88,188,201};
	list<int> yadayada(vals,vals+10);
	list<int> etcetera(vals,vals+10);
	cout<<"Original lists:\n";
	for_each(yadayada.begin(),yadayada.end(),outint);
	cout<<endl;
	for_each(etcetera.begin(),etcetera.end(),outint);
	cout<<endl;
	yadayada.remove_if(f100);
	etcetera.remove_if(TooBig<int>(200));
	cout<<"Trimmed lists:\n";
	for_each(yadayada.begin(),yadayada.end(),outint);
	cout<<endl;
	for_each(etcetera.begin(),etcetera.end(),outint);
	cout<<endl;				
	return 0;
}

 

 


发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注