智能指针:在对象被销毁的时候,new的内存也被销毁。不再用delete[]进行释放。
#include<iostream>
#include<string>
#include<memory> //必须包含的头文件
using namespace std;
class Report
{
private:
string str;
public:
Report(const string s)
{
str=s;
cout<<"Object is created!\n";
}
~Report()
{
cout<<"Object is deleted!\n";
}
void comment()
{
cout<<str<<endl;
}
};
int main()
{
{
auto_ptr<Report> ps(new Report("using auto_ptr"));
ps->comment();
}
{
shared_ptr<Report> ps(new Report("using shared_ptr"));
ps->comment();
}
{
unique_ptr<Report> ps(new Report("using unique_ptr"));
ps->comment();
}
return 0;
}
STL之vector
1.
#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main()
{
vector<string>item(5);
cout<<"enter:";
for(int i=0;i<5;i++)
getline(cin,item[i]);
cout<<"the strings are:\n";
for(int i=0;i<5;i++)
cout<<item[i]<<endl;
return 0;
}
2.自定义类:
#include<iostream>
#include<string>
#include<vector>
using namespace std;
class Review
{
private:
int rating;
string title;
public:
void fillReview();
void showReviw();
};
void Review::fillReview()
{
cout<<"Enter book title:\n";
getline(cin,title);
/*while(title!="q")
{
cout<<"Enter book rating:";
cin>>rating;
cout<<"Enter book title(q to quit):\n";
getline(cin,title);
}*/
cout<<"Enter book rating:";
cin>>rating;
while(cin.get()!='\n') //输入两个属性时 把cin.get()放在最后
continue;
}
void Review::showReviw()
{
cout<<title<<"\t"<<rating<<endl;
}
#include<iostream>
#include<vector>
#include<string>
#include"vector.h"
using namespace std;
int main()
{
vector<Review>books;//知识点
cout<<"Book1:\n";
Review r1;
r1.fillReview();
books.push_back(r1);//知识点
cout<<"Book2:\n";
Review r2;
r2.fillReview();
books.push_back(r2);
cout<<"Book3:\n";
Review r3;
r3.fillReview();
books.push_back(r3);
vector<Review>oldlist(books);
int num=books.size();
if(num>0)
{
cout<<"you have entered:\n"<<"Rating\tBook\n";
for(int i=0;i<num;i++)
books[i].showReviw();
cout<<"reshowing:\n"<<"Rating\tBook\n";
vector<Review>::iterator pt; //知识点
for(pt=books.begin();pt<books.end();pt++)
pt->showReviw();
}
vector<Review>::iterator pt;
books.erase(books.begin()+1,books.begin()+2);
cout<<"after erase:\n";
for(pt=books.begin();pt<books.end();pt++)
pt->showReviw();
books.swap(oldlist);
cout<<"after swap:\n";
for(pt=books.begin();pt<books.end();pt++)
pt->showReviw();
return 0;
}
3.迭代器的使用:
#include<iostream>
#include<iterator>
#include<vector>
#include<algorithm>//包含sort的头文件
using namespace std;
int main()
{
int casts[6]={6,7,2,9,4,11};
vector<int>dice(6);
copy(casts,casts+6,dice.begin());
cout<<"dice to be casts\n ";
sort(dice.begin(),dice.end());
ostream_iterator<int,char> out_iter(cout," ");//定义一个输出流迭代器
copy(dice.begin(),dice.end(),out_iter);//将向量的值送到输出流迭代器
cout<<endl;
cout<<"使用显式反转迭代器:\n";
copy(dice.rbegin(),dice.rend(),out_iter);
cout<<endl;
cout<<"使用隐式反转迭代器:\n";
vector<int>::reverse_iterator ri;
for(ri=dice.rbegin();ri!=dice.rend();ri++)
cout<<*ri<<" ";
cout<<endl;
cout<<"排序后:\n";
sort(dice.begin(),dice.end());
copy(dice.begin(),dice.end(),out_iter);
cout<<endl;
return 0;
}
插入:
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<iterator>
using namespace std;
void output(string s)
{
cout<<s<<" ";
}
int main()
{
string s1[4]={"fine","fish","fashion","fate"};
string s2[2]={"busy","bats"};
string s3[2]={"silly","singers"};
vector<string>words(4);
copy(s1,s1+4,words.begin());
for_each(words.begin(),words.end(),output);
cout<<endl;
//insert
copy(s2,s2+2,back_insert_iterator<vector<string>>(words));
for_each(words.begin(),words.end(),output);
cout<<endl;
copy(s3,s3+2,insert_iterator<vector<string> >(words,words.end()));
for_each(words.begin(),words.end(),output);
cout<<endl;
return 0;
}