9.4
#include <iostream>
#include <vector>
using namespace std;
bool find(auto begin,auto end,int num){
bool answer=1;
while(end!=begin){
if(*begin==num) return answer;
++begin;
}
answer=0;
return answer;
}
int main(){
vector<int> ivec;
int elem;
while(cin>>elem)
ivec.push_back(elem);
int num=5;
auto begin=ivec.begin(),end=ivec.end();
bool answer=find(begin,end,num);
if(answer) cout<<"true";
return 0;
}
C++ PRIMER 第五版 answer----9.5
#include <iostream>
#include <vector>
using namespace std;
vector<int> ivec;
decltype(ivec.begin()) find(auto begin,auto end,int num){
bool answer=1;
while(end!=begin){
if(*begin==num) return begin;
++begin;
}
return begin;
}
int main(){
int elem;
while(cin>>elem)
ivec.push_back(elem);
int num=5;
auto begin=ivec.begin(),end=ivec.end();
auto answer=find(begin,end,num);
if(end!=answer) cout<<*answer;
return 0;
}
9.6
list和单list不支持<运算符,甚至99页列出的算术运算符都不支持。他们和其他顺序容器不一样,不是按顺序存储,不适合去比较先后位置。改成判断相等会好点。
PS定义迭代器
vector<int>::iterator begin =ivec.begin(),end=ivec.end();
auto begin=ivec.begin(),end=ivec.end();
9.11
#include <iostream>
#include <vector>
using namespace std;
int main(){
vector<int> ivec;
vector<int> ivec1(ivec);
vector<int> ivec2{1,2,3};
vector<int> ivec3(ivec2.begin(),ivec2.end());
vector<int> ivec4(3);
vector<int> ivec5(3,4);
return 0;
}
9.12
①后者不知拷贝个数,故array不适用
②后者实质是一系列元素挨个进行初始化过程,对元素类型的要求减弱,只要能转换即可
9.13
#include <iostream>
#include <vector>
#include <list>
using namespace std;
int main(){
list<int> ivecc={1,2,3};
vector<double> ivec(ivecc.begin(),ivecc.end());
return 0;
}
9.18
#include <iostream>
#include <vector>
#include <deque>
using namespace std;
int main(){
string elem;
deque<string> ivec;
while(cin>>elem)
ivec.push_back(elem);
for(auto ix:ivec)
cout<<ix<<" ";
return 0;
}
9.19
#include <iostream>
#include <vector>
#include <list>
using namespace std;
int main(){
string elem;
list<string> ivec;
while(cin>>elem)
ivec.push_back(elem);
for(auto ix:ivec)
cout<<ix<<" ";
return 0;
}
9.20
#include <iostream>
#include <deque>
#include <list>
using namespace std;
int main(){
int elem;
list<int> ivec;
deque<int> odd;
deque<int> even;
while(cin>>elem)
ivec.push_back(elem);
for(auto ix:ivec){
if(ix%2==0) even.push_back(ix);
else odd.push_back(ix);
}
for(auto oddx:odd)
cout<<oddx<<" ";
cout<<endl;
for(auto evenx:even)
cout<<evenx<<" ";
return 0;
}
9.21
一样的,虽然vector没有push_front,但是这样相当了有时的效果
9.22
死循环
#include <iostream>
#include <vector>
using namespace std;
int main(){
vector<int> iv={1,2,3,4};
int num=2;
auto iter=iv.begin(),mid=iv.begin()+iv.size()/2;
while(iter!=mid){
if(*iter==num) iv.insert(iter,num);
iter++;
}
}
9.23
会是同一个值
9.24
#include <iostream>
#include <vector>
using namespace std;
int main(){
vector<string> svec={};
cout<<svec.at(0);
return 0;
}
terminate called after throwing an instance of 'std::out_of_range'
what(): vector::_M_range_check: __n (which is 0) >= this->size() (which is 0)
#include <iostream>
#include <vector>
using namespace std;
int main(){
vector<string> svec={};
cout<<svec[0];
return 0;
}
运行无输出,未报错
#include <iostream>
#include <vector>
using namespace std;
int main(){
vector<string> svec={};
cout<<svec.front();
cout<<svec.back();
return 0;
}
同上
front和back返回的是引用,begin和end返回是迭代器,解引用后获得元素的引用,即达到front和back相同的效果。
9.25
#include <iostream>
#include <vector>
using namespace std;
int main(){
vector<int> ivec={1,2,3,4,5,6,7,8,9};
auto elem1=ivec.begin()+2;
ivec.erase(elem1,elem1);
for(auto kx:ivec)
cout<<kx<<" ";
return 0;
}
范围相等,啥也没删;
#include <iostream>
#include <vector>
using namespace std;
int main(){
vector<int> ivec={1,2,3,4,5,6,7,8,9};
auto elem1=ivec.begin()+2,elem2=ivec.end();
ivec.erase(elem1,elem2);
for(auto kx:ivec)
cout<<kx<<" ";
return 0;
}
elem2是尾后迭代器,删了从elem1开始直到结束的全部元素
都是迭代器,也是啥也没删
删掉elem1和elem2之间实际上是删掉从elem1到elem2之前一个元素。左闭右开。
9.26
#include <iostream>
#include <vector>
#include <list>
using namespace std;
int main(){
int ia[]={0,1,1,2,3,5,8,13,21,55,89};
vector<int> ivec={0,1,1,2,3,5,8,13,21,55,89};
list<int> ilst(ivec.begin(),ivec.end());
auto it=ilst.begin();
while(it!=ilst.end())
if(*it%2) it=ilst.erase(it);
else it++;
for(auto kx:ilst)
cout<<kx<<" ";
cout<<endl;
auto ic=ivec.begin();
while(ic!=ivec.end())
if(*ic%2==0) ic=ivec.erase(ic);
else ic++;
for(auto kx:ivec)
cout<<kx<<" ";
return 0;
}
9.27
#include <iostream>
#include <forward_list>
using namespace std;
int main(){
forward_list<int> iford={1,2,3,4,5,6,7,8,9};
auto before_it=iford.before_begin();
auto it=iford.begin();
while(it!=iford.end())
if(*it%2) it=iford.erase_after(before_it);
else {before_it++;it++;} //before_it=it;it++;
for(auto kt:iford)
cout<<kt<<" ";
return 0;
}
疑问:有注释的那一行,注释内容是书上写法,是一定正确的,before_it++;it++; 是运行结果没有问题,但不知是否真正没有问题。并不清楚删除操作的影响。
#include <iostream>
#include <forward_list>
using namespace std;
forward_list<string> hanshu(forward_list<string> ssford,string fir,string sed){
auto before_sst=ssford.before_begin();
auto sst=ssford.begin();
while(sst!=ssford.end())
if(*sst==fir) {ssford.insert_after(sst,sed); break;}
else {before_sst++;sst++;}
if(sst==ssford.end()) ssford.insert_after(before_sst,sed);
return ssford;
}
int main(){
forward_list<string> sford={"hh","jk","gy","hg","ui","tj"};
string str1,str2;
cin>>str1>>str2;
sford=hanshu(sford,str1,str2);
for(auto kt:sford)
cout<<kt<<" ";
return 0;
}