9.23
都是容器内唯一的那个元素
9.24
int main(){
cout<<*vec9_24.begin();//运行异常,程序到此截断
cout<<vec9_24.front();//运行异常,程序到此截断
cout<<vec9_24[1]<<endl;//运行异常,程序到此截断
vec9_24.at(1);//显示out_of_range异常
return 0;
}
9.25
erase的两个迭代器参数相同什么都不会发生
erase的两个参数后一个为尾后迭代器则会从第一个迭代器一直删除到容器最后
9.26
int main()
{
int ia[]={0,1,1,2,3,5,8,13,21,55,89};
vector<int> vec9_26;
for(int i=0;i<sizeof(ia)/sizeof(ia[0]);i++)
vec9_26.push_back(ia[i]);
list<int> list9_26(vec9_26.begin(),vec9_26.end());
cout<<"list容器内容:"<<endl;
for(auto iter=list9_26.begin();iter!=list9_26.end();iter++){
cout<<*iter<<" ";
}
cout<<endl;
cout<<"list容器删除奇数后:"<<endl;
for(auto iter=list9_26.begin();iter!=list9_26.end();iter++){
if((*iter)%2==1){
iter=list9_26.erase(iter);
}
}
for(auto iter=list9_26.begin();iter!=list9_26.end();iter++){
cout<<*iter<<" ";
}
cout<<endl;
cout<<"vector容器内容:"<<endl;;
for(auto iter=vec9_26.begin();iter!=vec9_26.end();iter++){
cout<<*iter<<" ";
}
cout<<endl;
cout<<"vec容器删除奇数后:"<<endl;
for(auto iter=vec9_26.begin();iter!=vec9_26.end();iter++){
if((*iter)%2==0){
iter=vec9_26.erase(iter);
}
}
for(auto iter=vec9_26.begin();iter!=vec9_26.end();iter++){
cout<<*iter<<" ";
}
cout<<endl;
return 0;
9.27
int main()
{
// 基本与书上实例雷同
forward_list<int> flst = {0,1,2,3,4,6,5,7,8,9};
auto prev = flst.before_begin();
auto curr = flst.begin();
while(curr != flst.end()){
if(*curr%2==1){
curr=flst.erase_after(prev);
}else{
prev = curr;
curr++;
}
}
for(auto iter=flst.begin();iter!=flst.end();iter++){
cout<<*iter<<" ";
}
9.28
#include <iostream>
#include<vector>
#include<list>
#include<string>
#include<deque>
#include<forward_list>
#include<array>
#include<fstream>
using namespace std;
/**
*9.29练习题
*注意元素插入位置
*/
void find_insert(forward_list<string> &flst,string& str1,string &str2){
auto prev=flst.before_begin();
auto curr= flst.begin();
while(curr!=flst.end()){
if(*curr==str1){
flst.insert_after(curr,str2);
return;
}
//一定要用prev做参数,因为循环到最后的时候curr已经是尾后迭代器了,不能再尾后迭代器后插入元素
flst.insert_after(prev,str2);
prev=curr;
curr++;
}
}
int main()
{
forward_list<string> flast = {"wqe","asda","xxx","weds"};
string str1="xxx",str2="ssss";
find_insert(flast,str1,str2);
for(auto iter=flast.begin();iter!=flast.end();iter++){
cout<<*iter<<" ";
}
return 0;
}
9.29
在vec末尾添加75个值为0的元素
在vec末尾删除90个元素
9.30
若是内部数据类型添加的元素会初始化,若是外部数据类型必须要有默认的构造函数
9.31
int main(){
//9.31
list<int> li = {0,1,2,3,4,5,6,7,8,9};
auto iter9_31 = li.begin();
while(iter9_31!=li.end()){
if(*iter9_31%2){
iter9_31 = li.insert(iter9_31,*iter9_31);
iter9_31++;
iter9_31++; //向后移动迭代器,使其跳过复制的元素
}
else{
iter9_31 = li.erase(iter9_31);//删除偶数元素
}
}
//9.31单链表forward_list中删除元素时候需要两个迭代器
forward_list<int > flist = {0,1,2,3,4,5,6,7,8,9};
auto prev=flist.before_begin();
auto curr=flist.begin();
while(curr!=flist.end()){
if(*curr%2){
prev=curr;curr++;
prev=curr;curr++;
}else{
prev=flist.erase_after(prev);
}
}
}
9.32
iter++这个参数要改成(iter)++
9.33
将insert值赋予begin的时候 容器内的begin迭代器有效,不这样做的话迭代器只用第一次之后就失效了,
#include<vector>
#include<forward_list>
using namespace std;
int main()
{
vector<int> vec9_33= {1,2,3,4};
auto begin = vec9_33.begin();
while(begin!=vec9_33.end())
{
++begin;
begin=vec9_33.insert(begin,42);
++begin;
}
for (auto i : vec9_33)
cout << i << " ";
return 0;
}
9.34
int main()
{
vector<int> vec9_34= {1,2,3,4,5,6,47,8,89};
int i=1;
auto iter = vec9_34.begin();
while(iter!=vec9_34.end())
{
if(*iter%2==1){
iter=vec9_34.insert(iter,*iter);
cout<<i++<<endl;
}
iter++;
}
for (auto i : vec9_34)
cout << i << " ";
return 0;
}
通过观察控制台的i输出会发现程序无限循环,原因在于插入元素的时候没有将迭代器后移导致
要在if中加上一行
if(*iter%2==1){
iter=vec9_34.insert(iter,*iter);
iter++;//这是加上的一行
cout<<i++<<endl;
}
网友评论