C++primer plus第六版课后编程题答案18.5
2014-12-18 13:42
423 查看
第十八章答案均由热心博友:王琪胜 提供
邮箱: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;
}
邮箱: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;
}
相关文章推荐
- C++primer plus第六版课后编程题答案 6.8
- C++primer plus第六版课后编程题答案7.1
- C++primer plus第六版课后编程题答案7.5
- C++primer plus第六版课后编程题答案 6.5
- C++primer plus第六版课后编程题答案 6.3
- C++primer plus第六版课后编程题答案 6.8
- C++primer plus第六版课后编程题答案7.4
- C++primer plus第六版课后编程题答案7.5
- C++primer plus第六版课后编程题答案 6.3
- C++primer plus第六版课后编程题答案 6.5
- C++primer plus第六版课后编程题答案 6.9
- C++primer plus第六版课后编程题答案7.2
- C++primer plus第六版课后编程题答案 6.2
- C++primer plus第六版课后编程题答案 6.7
- C++primer plus第六版课后编程题答案 6.9
- C++PRIMER PLUS第六版课后编程题答案 3.1-3.7
- C++PRIMER PLUS第六版课后编程题答案 3.1-3.7
- C++primer plus第六版课后编程题答案 6.1
- C++primer plus第六版课后编程题答案 6.2
- C++primer plus第六版课后编程题答案7.3