当时刚学C++的时候买了这本书,一开始前面看的一知半解,索性就先缓缓,等学完学校的C++课程(中途自己也写了不少c++的代码),一段时间之后又拿起这本书去看,感觉还是挺有滋味的,这本书对我印象中的C++做了很大的扩展,个人认为这本书不太适合刚学C++就去看,而是写了一定的代码,对C++有一个大体的了解之后再去看会别有一番滋味(犇请无视这句话)。在看书的过程中自己也写了上面的课后练习题,现在整理一下,也跟大家分享一下,下面是9~12 15~16章的课后题编程题的答案
(第八章之前的都没保存/(ㄒoㄒ)/~~):
当时保存的时候是按节保存的,比如 练习9.3、练习9.4 练习9.5、练习9.6 是在9.2.1节练习里面的题
9.2.1节练习:
#include
#include
std::vector::iterator fun(std::vector::iterator isb,std::vector::iterator ise,int y){
while(isb s={1,2,3};
std::vector::iterator is=s.begin();
//if(fun(is,s.end(),2)) cout<<"OK"<
#include
#include
#include
#include
int main()
{
std::string temp;
std::dequedq;
for(int i=1;i<=3;++i){
std::cin>>temp;
dq.push_front(temp);
}
auto x=dq.begin(); //std::deque::iterator x;
while(x < dq.end() && std::cout<<" "<<*(x++));
std::cout<l;
for(int i=1;i<=3;++i){
std::cin>>temp;
l.push_front(temp);
}
auto y=l.begin();
while(y!=l.end() && std::cout<<" "<<*(y++)); //list中无 > <操作 使用!=代替
std::cout<
#include
#include
#include
#include
int main()
{
int ia[]={0,1,1,2,3,5,8,13,21,55,89};
std::vector vec={0,1,1,2,3,5,8,13,21,55,89};
std::list lis={0,1,1,2,3,5,8,13,21,55,89};
auto i=vec.begin();
while(i!=vec.end())
if(*i % 2)i=vec.erase(i);
else i++;
auto j=lis.begin();
while(j!=lis.end())
if(*j % 2==0)j=lis.erase(j);
else j++;
for(auto x:vec) std::cout<<" "<
练习9.47:
#include
#include
int main()
{
std::string temp("ab2c3d7R4");
std::string digit("0123456789");
std::string word;
for(int i=0;i<24;++i){
word+=(char)('a'+i);
word+=(char)('A'+i);
}
std::string::size_type pos=0;
while((pos=temp.find_first_of(word,pos))!=std::string::npos) std::cout<
#include
int main()
{
std::string temp("bdfghijklpqty");
std::string s;
while(std::cin>>s){
std::string::size_type pos=0;
while((pos=s.find_first_not_of(temp,pos))!=std::string::npos)std::cout<
#include
#include
int main()
{
std::vector vec={"1","2","3","4","5"};
int sum1=0;
for(auto x:vec) sum1+=stoi(x); //codeblock(12.11)报错,vs2012也报,本汪编译器不支持
std::cout<
练习9.51:
#include
class A{
public :
A(const std::string s="10/12/1994"){
std::string sign=" ,/";
auto pos=s.find_first_of(sign);
month=s.substr(0,pos);
day=s.substr(pos+1,s.find_first_of(sign,pos+1)-pos-1);
year=s.substr(s.find_last_of(sign)+1);
}
void print(){
std::cout<
#include
#include
#include
#include
#include
#include
using namespace std;
int main()
{
int a1[]={1,2,3,4,5,6};
int a2[sizeof(a1)/sizeof(a1)];
auto ret=copy(begin(a1),end(a1),begin(a2));
return 0;
}
*/
#include
#include
#include
#include
#include
using namespace std;
int main()
{
std::vector vec={1,2,3,4,5,6,7,12,3,4,6,7,3,6,9,2,6,3,3,3,3,3,3};
std::cout< lis={"q","e","sdg","zvgs","123g","545","qwe","uyt","qwe"};
std::cout<
/*
#include
#include
#include
#include
#include
using namespace std;
class A{
public :
A(const string s="1/1/1990"):month("jan"),day("1"),year("1994"){
string sign=" ,/";
auto pos=s.find_first_of(sign);
month=s.substr(0,pos);
day=s.substr(pos+1,s.find_first_of(sign,pos+1)-pos-1);
year=s.substr(s.find_last_of(sign)+1);
}
void print(){
cout<
#include
#include
#include
int main()
{
std::vector vec={1,2,3,4,5,6,7,8,9};
std::cout< vec_str={"a","b","c","d"};
std::cout<
10.2.2节练习:
#include
#include
#include
#include
#include
int main()
{
//10.6
int num[]={1,2,3,4,5,6};
std::fill_n(std::begin(num),std::end(num)-std::begin(num),0);
for(auto x:num) std::cout< vec;
std::listlst;
int i;
while(std::cin>>i) lst.push_back(i);
std::copy(lst.cbegin(),lst.cend(),back_inserter(vec)); //vec未申请空间,所以使用插入迭代器
for(auto x:vec) std::cout< vecc;
vecc.reserve(10);
//虽然分配了内存 但size()=0 vecc.begin()==vecc.end() capacity()=10
std::fill_n(vecc.begin(),10,0);
for(auto x:vecc) std::cout<
10.2.3节练习:
#include
#include
#include
#include
#include
void elimDups(std::vector &vec){
std::sort(vec.begin(),vec.end());
auto uni=std::unique(vec.begin(),vec.end()); //把重复单词忘末尾放,返回指向不重复范围的后一个迭代器
vec.erase(uni,vec.end());
for(auto x:vec) std::cout< vec={"the","quick","red","fox","jumps","over","the","slow","red","turtle"};
elimDups(vec);
return 0;
}
// the quick red fox jumps over the slow red turtle
10.3.1节练习:
#include
#include
#include
#include
#include
void elimDups(std::vector &vec){
std::sort(vec.begin(),vec.end());
auto uni=std::unique(vec.begin(),vec.end()); //把重复单词忘末尾放,返回指向不重复范围的后一个迭代器
vec.erase(uni,vec.end());
for(auto x:vec) std::cout< vec={"the","quick","red","fox","jumps","over","the","slow","red","turtle"};
elimDups(vec);
std::stable_sort(vec.begin(),vec.end(),isShortter);
for(auto x:vec) std::cout<
练习10.16:
#include
#include
#include
void elimDups(std::vector &vec){
std::sort(vec.begin(),vec.end());
auto uni=std::unique(vec.begin(),vec.end()); //把重复单词忘末尾放,返回指向不重复范围的后一个迭代器
vec.erase(uni,vec.end());
std::cout<<"after unique_sort and erase :"<>x>>y;
auto fun2=[x](const int y){return x+y;};
std::cout< vec={"the","quick","red","fox","jumps","over","the","slow","red","turtle"};
elimDups(vec);
std::stable_sort(vec.begin(),vec.end(),[](const std::string &a,const std::string &b){return a.size()>n;
auto iter=find_if(vec.begin(),vec.end(),
[n](const std::string &s){
return s.size()>=n;
});
auto count=vec.end()-iter;
for_each(iter,vec.end(),[](const std::string &s){std::cout<
练习10.18:
#include
#include
#include
void elimDups(std::vector &vec){
std::sort(vec.begin(),vec.end());
auto uni=std::unique(vec.begin(),vec.end()); //把重复单词忘末尾放,返回指向不重复范围的后一个迭代器
vec.erase(uni,vec.end());
std::cout<<"after unique_sort and erase :"< vec={"the","quick","red","fox","jumps","over","the","slow","red","turtle"};
elimDups(vec);
stable_sort(vec.begin(),vec.end(),[](const std::string &a,const std::string &b){return a.size()>n;
auto iter=partition(vec.begin(),vec.end(),
[n](const std::string &s){
return s.size()
10.3.3节练习:
#include
#include
#include
int main()
{
std::vector vec={"the","quick","red","fox","jumps","over","the","slow","red","turtle"};
auto x=count_if(vec.begin(),vec.end(),[](const std::string &s){
return s.size()>=6;});
std::cout<
#include
#include
#include
#include
using namespace std::placeholders; // bind_n操作
bool check_size(const int &x,std::string::size_type sz){return x>=sz;}
int main()
{
std::vector vec={1,3,6,8,0,3,5,12,4,2};
std::string s="woqu";
auto f=bind(check_size,_1,s.size());
for(auto x:vec)
if(f(x)){
std::cout<
10.4.1节练习:
#include
#include
#include
#include
#include
int main()
{
//10.27
std::list lst={1,2,2,2,2,2,2,2,6,8};
std::list lst1;
unique_copy(lst.begin(),lst.end(),back_inserter(lst1));
std::cout<<"old array:";
for_each(lst.begin(),lst.end(),[](const int &x){std::cout< vec={1,2,3,4,5,6,7,8,9};
std::vector front_vec,back_vec,rand_vec;
auto iter=vec.begin();
//copy(vec.begin(),vec.end(),front_inserter(front_vec));
copy(vec.begin(),vec.end(),back_inserter(back_vec));
copy(vec.begin(),vec.end(),inserter(vec,iter));
// std::cout<<"front:";
//for_each(front_vec.begin(),front_vec.end(),[](const int &x){std::cout<
10.4.2节练习:
#include
#include
#include
#include
#include
//10.33
void fun(const std::string &inf_file,const std::string &outf1_file,const std::string &outf2_file){
std::ifstream inf;
inf.open(inf_file);
std::istream_iterator inf_iter(inf),eof;
std::ofstream outf1;
outf1.open(outf1_file);
std::ofstream outf2;
outf2.open(outf2_file);
std::ostream_iterator outf1_iter(outf1),outf2_iter(outf2);
while(inf_iter!=eof){
if((*inf_iter)%2) outf1<<*inf_iter;
else outf2<<*inf_iter;
inf_iter++;
}
std::cout<<"从文件\""< in_iter(inf); //in_ter 从文件流inf中读取类型为std::string的值
std::istream_iterator eof; //文件结束位置
std::vector vec(in_iter,eof); //把文件的字符串读入vec中
auto iter=vec.begin();
while(iter!=vec.end()) std::cout<<*iter++<<" ";
std::cout< in(std::cin); //in从输入流cin中读取类型为int的值
std::istream_iterator eof2; //输入流结束位置
std::vector vec2(in,eof2); //输入的数据读入vec2
sort(vec2.begin(),vec2.end()); //排序
std::ostream_iterator out(std::cout," "); //把类型为int的值写到输出流out中,每个值后加空格
copy(vec2.begin(),vec2.end(),out);//vec2的值读出到out中
std::cout< out2(std::cout," ");
unique_copy(vec2.begin(),vec2.end(),out2); //不重复的复制
std::cout<
10.4.3节练习:
#include
#include
#include
#include
#include
int main()
{
//10.34 10.35
std::vector vec={1,2,3,4,5,6,7,8,9};
std::vector::reverse_iterator reiter=vec.rbegin(); //反向迭代器
while(reiter!=vec.rend()) std::cout<<*reiter++<<" ";
std::cout<::iterator iter=vec.begin(); //普通迭代器
while(iter!=vec.end()) std::cout<<*iter++<<" ";
std::cout< lis={1,2,3,0,6,4,0,1,3,2}; //查找最后一个0,返回其迭代器
std::list::reverse_iterator lis_reiter=find(lis.rbegin(),lis.rend(),0);
std::cout<<*lis_reiter< vec2={1,2,3,4,5,6,7,8,9,10}; // 取其中 3~7 的位置复制给std::list
std::list lis2;
auto vec2_iter=vec2.begin();
copy(vec2_iter+2,vec2_iter+7,back_inserter(lis2));//后插迭代器实现
for(auto x:lis2) std::cout<
11.1.1节练习:
#include
#include
#include
#include
#include
#include
#include
#include
11.2.3节练习~练习11.12、练习11.13:
#include
#include
#include
#include
#include
练习11.14:
#include
#include
#include
#include //pair
#include
11.3.2节练习:
#include
#include
#include
11.3.5节练习:
#include
#include
#include
#include
11.3.6节练习:
#include
#include
#include
#include
#include
#include
#include
//12.2
class StrBlob{
typedef std::vector vstring;
public:
StrBlob():data(std::make_shared()){}
StrBlob(std::initializer_list lis):data(std::make_shared(lis)){}
vstring::size_type size()const {return data->size();}
void push_back(const std::string &s){data->push_back(s);}
void pop_back(){
check(0,"pop on empty StrBlob");
data->pop_back();
}
std::string &front(){
check(0,"front on empty StrBlob");
return data->front();
}
std::string &back(){
check(0,"back on empty StrBlob");
return data->back();
}
private:
std::shared_ptr data; // emory
void check(vstring::size_type i,const std::string &s)const {
if(data->size()<=i) throw std::out_of_range(s); // stdexcept
}
};
int main()
{
//12.1
StrBlob b1{};
{
StrBlob b2={"a","an","the"};
b1=b2;
b2.push_back("about");
}
std::cout<
12.1.2节练习:
#include
#include
#include
//12.6
std::vector *new_vecint(){
return new std::vector;
}
void read_data(std::istream &in,std::vector *&pv){ //普通指针 引用指针
int x;
in>>x;
pv->push_back(x);
}
void print(std::vector *&pv){
for(auto x:*pv) std::cout< > ptr_vecint(){ //智能指针
return std::make_shared > ();
//return shared_ptr> (new vector(p));
}
void ptr_read_data(std::istream &in,std::shared_ptr > &spv){
int x;
in>>x;
spv->push_back(x);
}
void ptr_print(std::shared_ptr > spv){
for(auto x:*spv) std::cout<
12.1.6节练习:
#include
#include
#include
#include
#include
#include
#include
class StrBlob{
typedef std::vector vstring;
public:
friend class StrBlobPtr;
StrBlob():data(std::make_shared()){}
StrBlob(std::initializer_list lis):data(std::make_shared(lis)){}
vstring::size_type size()const {return data->size();}
void push_back(const std::string &s){data->push_back(s);}
void pop_back(){
check(0,"pop on empty StrBlob");
data->pop_back();
}
std::string &front(){
check(0,"front on empty StrBlob");
return data->front();
}
std::string &back(){
check(0,"back on empty StrBlob");
return data->back();
}
private:
std::shared_ptr data; // memory
void check(vstring::size_type i,const std::string &s)const {
if(data->size()<=i) throw std::out_of_range(s); //stdexcept
}
};
//12.19 12.20
class StrBlobPtr{
typedef std::vector vstring;
public :
StrBlobPtr():curr(0){}
StrBlobPtr(StrBlob &a,size_t sz=0):wptr(a.data),curr(sz){}
std::string &deref() const {
auto p=check(curr,"dereference past end");
return (*p)[curr];
}
StrBlobPtr& incur(){
check(curr,"increment past end of StrBlobPtr");
++curr;
return *this;
}
bool empty(){
if(curr < wptr.lock()->size()) return false;
return true;
}
private:
std::shared_ptr check(size_t i,const std::string &msg)const {
auto ret=wptr.lock();
if(!ret) throw std::runtime_error("unbound StrBlobPtr");
if(ret->size() <= i)
throw std::out_of_range(msg);
return ret;
}
std::weak_ptr wptr;
size_t curr;
};
int main()
{
//12.20
std::ifstream in("in.txt");
std::string word_s;
StrBlob sb;
while(getline(in,word_s)){ //从文件读入数据
std::stringstream sin(word_s);
std::string word;
while(sin>>word) sb.push_back(word);
sb.push_back("\n");
}
StrBlobPtr sbp(sb); //使用StrBlob 指针类型输出StrBlob
while(!sbp.empty()){
std::cout<
12.2.1节练习:
#include
#include
#include
#include
#include
int main()
{
//12.23
char sc1[]="woqu",sc2[]=" nimeide";
char *q=new char[strlen(sc1)+strlen(sc2)+1];
strcpy(q,sc1);
strcat(q,sc2);
std::cout<> size;
char* input = new char[size + 1]();
std::cin.ignore(); //读取多余回车
std::cout << "input the string: ";
std::cin.get(input, size + 1);
std::cout << input;
delete[] input;
return 0;
}
#include
#include
//12.26
int main()
{
int n;
std::cout<<"please input size:";
std::cin>>n;
std::cin.ignore();
std::allocator t; //定义类型对象
auto const p = t.allocate(n); //申请内存
auto q=p;
std::string word;
while(q!=p+n && std::cin>>word) t.construct(q++,word); //调用构造函数
while(q!=p){
std::cout<<*--q<
12.3.1节练习:
#include
#include
12.3.2节练习:
练习12.30:
#include
#include
练习12.32:
#include
#include
练习12.33:
#include
#include
15.2.1节练习:
#include
//15.3
class Quote{
public :
Quote()=default;
Quote(const std::string &book,double sales_price):bookNo(book),price(sales_price){}
std::string isbn()const {return bookNo;}
virtual double net_price(std::size_t n) const {return n*price;}
friend double print_total(std::ostream&,const Quote&,size_t );
private:
std::string bookNo;
protected:
double price=0.0;
};
double print_total(std::ostream &os,const Quote &item,size_t n){
double ret=item.net_price(n);
os<<"isbn: "<
15.2.2节练习:
#include
#include
//15.5 15.6 15.7
class Quote{
public :
Quote()=default;
Quote(const std::string &book,double sales_price):bookNo(book),price(sales_price){}
std::string isbn()const {return bookNo;}
virtual double net_price(std::size_t n) const {return n*price;}
friend double print_total(std::ostream&,const Quote&,size_t );
private:
std::string bookNo;
protected:
double price=0.0;
};
class Bulk_quote:public Quote{
public :
Bulk_quote()=default;
Bulk_quote(const std::string &book,double sales_price,std::size_t num,double disc):
Quote(book,sales_price),min_num(num),discount(disc){};
double net_price(std::size_t n) const{
if(n>=min_num) return n*price*discount;
else return n*price;
}
private:
std::size_t min_num;
double discount;
};
class huodong_quote:public Quote{
public :
huodong_quote()=default;
huodong_quote(const std::string book,double sales_price,std::size_t num,double disc):
Quote(book,sales_price),max_num(num),discount(disc){}
double net_price(std::size_t n)const {
if(n<=max_num) return n*price*discount;
else return net_price(max_num)+(n-max_num)*price;
}
private:
double discount;
size_t max_num;
};
double print_total(std::ostream &os,const Quote &item,size_t n){
double ret=item.net_price(n);
os<<"isbn: "<
15.3节练习:
#include
#include
//15.11 debug()
class Quote{
public :
Quote()=default;
Quote(const std::string &book,double sales_price):bookNo(book),price(sales_price){}
std::string isbn()const {return bookNo;}
virtual double net_price(std::size_t n) const {return n*price;}
friend double print_total(std::ostream&,const Quote&,size_t );
virtual void debug(){
std::cout<<"类成员及类型如下所示:"<=min_num) return n*price*discount;
else return n*price;
}
void debug()override{
Quote::debug();
std::cout<<"size_t : min_num"<
15.4节练习:
#include
#include
//15.15 15.16
class Quote{
public :
Quote()=default;
Quote(const std::string &book,double sales_price):bookNo(book),price(sales_price){}
std::string isbn()const {return bookNo;}
virtual double net_price(std::size_t n) const {return n*price;}
friend double print_total(std::ostream&,const Quote&,size_t );
private:
std::string bookNo;
protected:
double price=0.0;
};
double print_total(std::ostream &os,const Quote &item,size_t n){
double ret=item.net_price(n);
os<<"isbn: "<=off_num) return n*price*discount;
else return n*price;
}
};
class huodong_quote:public Disc_quote{
public :
huodong_quote()=default;
huodong_quote(const std::string book,double sales_price,std::size_t num,double disc):
Disc_quote(book,sales_price,num,disc){}
double net_price(std::size_t n)const override{
if(n<=off_num) return n*price*discount;
else return net_price(off_num)+(n-off_num)*price;
}
};
int main()
{
Bulk_quote b("高数",20,5,0.5);
huodong_quote h("线代",20,5,0.5);
print_total(std::cout,b,6);
print_total(std::cout,h,6);
return 0;
}
15.5节练习:
#include
#include
//15.21 15.22
class Graph{
public :
Graph()=default;
Graph(double x):r(x){}
virtual double get_area()=0;
virtual double get_value()=0;
protected:
double r;
const double Pi=3.1415926;
};
class Grid:public Graph{
public :
Grid()=default;
Grid(double x):Graph(x){}
double get_area(){return r*r;}
double get_value(){return 0.0;}
};
class Circle:public Graph{
public :
Circle()=default;
Circle(double x):Graph(x){}
double get_area(){return Pi*r*r;}
double get_value(){return 0.0;}
};
class Ball:public Graph{
public :
Ball()=default;
Ball(double x):Graph(x){}
double get_area(){return 4.0*Pi*r*r;}
double get_value(){return 4.0*Pi*r*r*r/3.0;}
};
class Cone:public Graph{
public :
Cone()=default;
Cone(double x,double y):Graph(x),height(y){}
double get_area(){return Pi*r*r+Pi*r*sqrt(height*height+r*r);}
double get_value(){return Pi*height*r*r/3.0;}
protected:
double height;
};
int main()
{
Grid g(2.0);
Circle ci(2.0);
Ball b(2.0);
Cone co(2.0,3.0);
std::cout<<"Grid : "<
15.7.3节练习:
#include
//15.26
class Quote{
public :
Quote()=default;
Quote(const std::string &book,double sales_price):bookNo(book),price(sales_price){
std::cout<<"Quote construct "<bookNo=qu.bookNo;
this->price=qu.price;
std::cout<<"Quote copy "<min_num=bkq.min_num;
this->discount=bkq.discount;
std::cout<<"Bulk_quote copy "<
15.8节练习:
#include
#include
#include
#include
//15.28 15.29
class Quote{
public :
Quote()=default;
Quote(const std::string &book,double sales_price):bookNo(book),price(sales_price){}
std::string isbn()const {return bookNo;}
virtual double net_price(std::size_t n) const {return n*price;}
friend double print_total(std::ostream&,const Quote&,size_t );
private:
std::string bookNo;
protected:
double price=0.0;
};
class Bulk_quote:public Quote{
public :
Bulk_quote()=default;
Bulk_quote(const std::string &book,double sales_price,std::size_t num,double disc):
Quote(book,sales_price),min_num(num),discount(disc){};
double net_price(std::size_t n) const{
if(n>=min_num) return n*price*discount;
else return n*price;
}
private:
std::size_t min_num;
double discount;
};
double print_total(std::ostream &os,const Quote &item,size_t n){
double ret=item.net_price(n);
os<<"isbn: "< vec1;
vec1.push_back(Bulk_quote("高数",20,8,0.5));//只是把其中Quote的部分拷贝给vec1了
vec1.push_back(Bulk_quote("高数",20,5,0.5));
double sum1=0.0;
for(auto x:vec1){
sum1+=print_total(std::cout,x,7);
}
std::cout<<"total_price:"< > vec2;
vec2.push_back(std::make_shared("高数",20,8,0.5)); //指针动态绑定
vec2.push_back(std::make_shared("高数",20,5,0.5));
double sum2=0.0;
for(auto x:vec2){
sum2+=print_total(std::cout,*x,7);
}
std::cout<<"total_price:"<
15.8.1节练习:
#include
#include
#include
//15.30
class Quote{
public :
Quote()=default;
Quote(const std::string &book,double sales_price):bookNo(book),price(sales_price){}
std::string isbn()const {return bookNo;}
virtual double net_price(std::size_t n) const {return n*price;}
friend double print_total(std::ostream&,const Quote&,size_t );
// virtual Quote *clone()const &{return new Quote(*this);} 编译器不支持
private:
std::string bookNo;
protected:
double price=0.0;
};
class Bulk_quote:public Quote{
public :
Bulk_quote()=default;
Bulk_quote(const std::string &book,double sales_price,std::size_t num,double disc):
Quote(book,sales_price),min_num(num),discount(disc){};
//Bulk_quote *clone()const &{return new Bulk_quote(*this);}
double net_price(std::size_t n) const{
if(n>=min_num) return n*price*discount;
else return n*price;
}
private:
std::size_t min_num;
double discount;
};
double print_total(std::ostream &os,const Quote &item,size_t n){
double ret=item.net_price(n);
os<<"isbn: "< sale){ item.insert(sale); }
//void add_item(const Quote &sale){
// item.insert(std::shared_ptr(sale.clone()));
//}
//输出
double total_receipt(std::ostream& os)const{
double sum=0.0;
//upper_bound(*iter) 指向下一个关键字
//item.count(*iter) 计算关键字为*iter的元素个数
for(auto iter=item.cbegin();iter!=item.cend();iter=item.upper_bound(*iter)){
sum+=print_total(os,**iter,item.count(*iter));
}
os<<"Total Sale: "< &lhs,const std::shared_ptr &rhs){
return lhs->isbn()isbn();
}
std::multiset,decltype(cmp)*> item{cmp};
};
int main()
{
Basket b;
for(int i=1;i<=5;++i) b.add_item(std::make_shared("高数",20,5,0.5));
for(int i=1;i<=7;++i) b.add_item(std::make_shared("线代",30,5,0.5));
//for(int i=1;i<=5;++i) b.add_item(Bulk_quote("高数",20,5,0.5));
//for(int i=1;i<=7;++i) b.add_item(Bulk_quote("线代",30,5,0.5));
b.total_receipt(std::cout);
return 0;
}
#include
#include
#include
#include
#include
#include
#include
//16.2
template int compare(const T& a,const T& b){
if(a>b) return 1;
else if(a v1={1,2,3,4},v2={2,3,4};
std::cout< T1 find1(T1 beg,T1 end,const T2& temp){
while(beg!=end)
if(*beg==temp) return beg;
else ++beg;
return end;
}
void fun4(){
std::vector a={1,3,4,5};
auto iter=find1(a.begin(),a.end(),3);
std::cout<<*iter< ls={"wo","qu","dou","bi"};
auto iter2=find1(ls.begin(),ls.end(),"qu");
std::cout<<*iter2< void print(const T (&p)[N]){
for(int i=0;i T* begin(T (&p)[N]){
if(N>1) return p;
else return NULL;
}
template T* end(T (&p)[N]){
if(N>1)return p+N;
else return NULL;
}
void fun6(){
int num[]={1,2,3,4,5};
char s[]="woqufule";
std::for_each(begin(num),end(num),
[](const int &x){std::cout< constexpr unsigned size_array(T (&p)[N]){
return N;
}
void fun7(){
int num[]={1,2,3,4};
std::cout<
16.1.2节练习:
#include
#include
#include
#include
#include
//16.12
template class BlobPtr;
template class Blob{
friend BlobPtr ;
public:
typedef typename std::vector::size_type size_type;
Blob():data(std::make_shared >()){}
Blob(std::initializer_list lis):data(std::make_shared >(lis)){}
size_type size()const {return data->size();}
void push_back(const T &s){data->push_back(s);}
void pop_back(){
check(0,"pop on empty Blob");
data->pop_back();
}
T &front(){
check(0,"front on empty Blob");
data->front();
}
T &back(){
check(0,"back on empty Blob");
return data->back();
}
T &operator[](size_type t);
private:
std::shared_ptr > data; // emory
void check(size_type i,const std::string &s)const {
if(data->size()<=i) throw std::out_of_range(s); // stdexcept
}
};
template
T &Blob::operator[](size_type t){
check(t,"subscript out of range");
return (*data)[t];
}
template class BlobPtr{
public :
BlobPtr():curr(0){}
BlobPtr(Blob &a,size_t sz=0):wptr(a.data),curr(sz){}
T &operator*()const{
auto p=check(curr,"derefence past end");
return (*p)[curr];
}
BlobPtr& operator++(){
BlobPtr ret=*this;
curr++;
// ++*this;
return ret;
}
BlobPtr& operator--(){
BlobPtr ret=*this;
curr--;
// --*this;
return ret;
}
private:
std::shared_ptr > check(std::size_t t,const std::string &msg)const {
auto ret=wptr.lock();
if(!ret) throw std::runtime_error("unbound BlobPtr");
else if(t>=ret->size())throw std::out_of_range(msg);
return ret;
}
std::weak_ptr > wptr;
std::size_t curr;
};
int main()
{
//16.12
Blob b1={"a","an","the"};
for(auto i=0;i pb(b1);
for(int i=0;i
16.1.2节练习:
#include
#include
//16.14 16.15
template class Screen{
typedef std::string::size_type pos;
public:
Screen()=default;
Screen(char c):contents(H*W,c){}
char get()const { return contents[cursor];} //取当前焦点的值
Screen &move(pos r,pos c){ //移动焦点
if(r<1) throw std::out_of_range("out of screen");
cursor =(r-1)*width +c;
return *this;
}
Screen &operator<<(const char &c){ //输出当前焦点的值
contents[cursor] =c;
return *this;
}
Screen &operator>>(char &c){ //输入当前焦点的值
c=contents[cursor];
return *this;
}
friend std::ostream &operator<<(std::ostream &os,const Screen &c ){ //输出屏幕
for(int i=0;i src('x');
src.move(5,5);
src<<'c';
std::cout<
16.1.3节练习:
#include
#include
#include
#include
//16.19
template void print(T& pack){
for(auto &x:pack) std::cout< void print2(T& pack){
for(auto it=pack.begin();it!=pack.end();++it) std::cout<<*it<<" ";
std::cout< vec={1,2,3,4,5};
print(vec);
print2(vec);
std::list li={'w','o','q','u'};
print(li);
print2(li);
std::set st={6,7,8};
print(st);
print2(st);
return 0;
}
#include
//16.21
class DebugDelete{
public :
DebugDelete(std::ostream &s = std::cerr):os(s){} //构造函数
template void operator()(T *p)const{ //重载()运算符
os<<"deleteing unique_ptr"<
16.1.4节练习:
#include
#include
#include
#include
template class Blob{
public:
typedef typename std::vector::size_type size_type;
Blob():data(std::make_shared >()){}
Blob(std::initializer_list lis):data(std::make_shared >(lis)){}
size_type size()const {return data->size();}
void push_back(const T &s){data->push_back(s);}
void pop_back(){
check(0,"pop on empty Blob");
data->pop_back();
}
T &front(){
check(0,"front on empty Blob");
data->front();
}
T &back(){
check(0,"back on empty Blob");
return data->back();
}
T &operator[](size_type t);
//16.24
Blob(typename std::vector::iterator b,typename std::vector::iterator e){
data=std::make_shared >(b,e);
}
private:
std::shared_ptr > data; // emory
void check(size_type i,const std::string &s)const {
if(data->size()<=i) throw std::out_of_range(s); // stdexcept
}
};
int main()
{
std::vector v={1,2,3};
Blob b(v.begin(),v.end());
std::cout<