十六章
//
// main.cpp
// Sixteen
//
// Created by amengdev on 16/3/13.
// Copyright © 2016年 amengdev. All rights reserved.
//
/*
C++primer Plus
十六章:string类和标准模板库
*/
/*
struct Review{
string title;
int rating;
};
//函数定义
//填充Review函数
bool FillReview(Review & rr);
//输出Review函数
void ShowReview(const Review &rr);
//函数实现
//填充Reivew函数
bool FillReview(Review & rr)
{
cout<<"Enter the title(quit to quit)";
getline(cin,rr.title);
if(rr.title=="quit")
return false;
cout<<"Enter the book rating";
cin>>rr.rating;
if(!cin)
return false;
while (cin.get()!='\n') {
continue;
}
return true;
}
//输出Review函数
void ShowReview(const Review & rr)
{
cout<<"Title:"<<rr.title<<" Rating:"<<rr.rating<<endl;
}
int main()
{
// C++vector容器的使用
// 1:容器的创建
// 2:容器的填充push_back与输出
// 3:迭代器的使用 vector<Review>::iterator it=books.begin();
// 4:容器元素的添加和删除books.insert(),books.erase()
// 5:容器交换 books.swap();
vector<Review> books;
Review temp;
while(FillReview(temp))
{
books.push_back(temp);
}
vector<Review> oldbooks=books;
int num=books.size();
if(num>0)
{
cout<<"---------"<<endl;
for(int i=0;i<num;i++)
{
ShowReview(books[i]);
}
cout<<"-------"<<endl;
for(vector<Review>::iterator it=books.begin();it!=books.end();it++)
{
ShowReview(*it);
}
cout<<"-------"<<endl;
books.erase(books.begin()+1,books.begin()+2);
for(vector<Review>::iterator it=books.begin();it!=books.end();it++)
{
ShowReview(*it);
}
cout<<"-------"<<endl;
books.insert(books.begin(), oldbooks.begin()+1,oldbooks.begin()+2);
for(vector<Review>::iterator it=books.begin();it!=books.end();it++)
{
ShowReview(*it);
}
}
cout<<endl;
books.swap(oldbooks);
for(vector<Review>::iterator it=books.begin();it!=books.end();it++)
{
ShowReview(*it);
}
return 0;
}
*/
/*
int main()
{
// C++三种智能指针赋值操作的区别
// 1:auto_ptr和unique_ptr赋值操作是转让指针所有权,被转让的指针不能正常使用
// 2:shared_ptr是增加指针计数器,每赋值一次,指针计数器++,等所有只能指针过期后,才会调用delete函数
// 3:auto_ptr和unique_ptr都是转让指针所有权,但是unique_ptr更安全,因为unique_ptr是在编译的时候直接报错,而auto_ptr是在执行的时候报错,编译报错比执行报错更容易发现和修改
shared_ptr<string> sp[5]={
shared_ptr<string>(new string("one")),
shared_ptr<string>(new string("two")),
shared_ptr<string>(new string("three")),
shared_ptr<string>(new string("four")),
shared_ptr<string>(new string("five"))
};
shared_ptr<string> sp0;
sp0=sp[2];
for(int i=0;i<5;i++)
{
cout<<*sp[i]<<endl;
}
cout<<*sp0<<endl;
}
*/
/*
class Report
{
private:
string str;
public:
Report(string s):str(s){cout<<"crate Report"<<endl;}
void fun(){cout<<str<<endl;}
~Report(){cout<<"delete Report"<<endl;}
};
int main()
{
// C++三种智能指针的使用
// 1:在块中定义的只能指针,块结束时自动销毁
// 2:只能指针的定义方式
{
auto_ptr<Report> ap(new Report("123"));
ap->fun();
}
{
unique_ptr<Report> up(new Report("456"));
up->fun();
}
{
shared_ptr<Report> sp(new Report("789"));
sp->fun();
}
return 0;
}
*/
/*
int main()
{
// C++string初始化赋值的8种方法
// 1:具体字符串初始化赋值
string one("one");
cout<<one<<endl;
// 2:赋值n个字符c的字符串
string two(10,'c');
cout<<two<<endl;
// 3:以string字符串表达式赋值
string three(one);
cout<<three<<endl;
// 4:初始化为空,用算数运算符表达式赋值
string four;
four=one+two;
cout<<four<<endl;
// 5:根据数组赋值,赋值得到数组的前n个元素
char arr[20]="1234567890123456789";
string five(arr,10);
cout<<five<<endl;
// 6:根据数组赋值,赋值得到数组的第m个到第n个元素
string six(arr+5,arr+10);
cout<<six<<endl;
// 7:根据字符串起始地址和结束地址赋值
string seven(&five[5],&five[10]);
cout<<seven<<endl;
// 8:根据字符串起始元素下标和结束元素下标赋值
string eight(five,5,10);
cout<<eight<<endl;
return 0;
}
*/