第十八章答案均由热心博友:王琪胜 提供
邮箱:wqs1639@gmail.com
如果对答案有疑问的可以发Email给我或者该博友,感谢大家一直以来的支持,
祝大家都能在编程中找到自己的最亮的那个闪光点。
————————————————————————————————————————————————————
18.4
//functor.cpp--using a-functor
#include <iostream>
#include <list>
#include <iterator>
#include <algorithm>
int main(){
using namespace std;
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(),[](int x){std::cout<<x<<" ";});
cout<<endl;
for_each(etcetera.begin(),etcetera.end(),[](int x){std::cout<<x<<" ";});
cout<<endl;
yadayada.remove_if([](int x){return x>100;});
etcetera.remove_if([](int x){return x>200;});
cout<<"Trimmed lists:\n";
for_each(yadayada.begin(),yadayada.end(),[](int x){std::cout<<x<<" ";});
cout<<endl;
for_each(etcetera.begin(),etcetera.end(),[](int x){std::cout<<x<<" ";});
cout<<endl;
return 0;
}