9.1
(a) list
(b) deque
(c) vector
9.2 list
bool find(vector<int>::iterator begin, vector<int>::iterator end, int target)
{
bool flag=0;
for (; begin != end; ++begin)
{
if (*begin == target)
{
flag = 1;
break;
}
}
return flag;
}
9.5
vector<int>::iterator find(vector<int>::iterator begin, vector<int>::iterator end, int target)
{
bool flag=0;
for (; begin != end; ++begin)
{
if (*begin == target)
return begin;
}
return end;
}
9.6 略
9.7 vector::size_type
9.8 list::const_reference和list::reference
9.9 一个返回普通迭代器,另一个返回const类型迭代器
9.10 it1 vector::iterator
it2 const vector::iterator
it3 const vector::iterator
it4 const vector::iterator
9.11 略
9.12 接受一个容器的构造函数,两个容器类型和元素类型都必须匹配
接收迭代器的构造函数,可以不匹配,元素只要能转换即可
9.13 使用迭代器。
9.14 用assign成员函数
9.15-9.17 略
9.18
#include <iostream>
#include <deque>
using namespace std;
int main()
{
string temp;
deque<string> str;
while (cin >> temp)
{
str.push_back(temp);
}
auto b = str.begin();
auto c = str.end();
for (; b != c; b++)
{
cout << *b << ends;
}
cout << endl;
9.19 不需要多少改变
9.20
#include <iostream>
#include <deque>
#include <list>
using namespace std;
int main()
{
deque<int> deq_first, deq_second;
list<int> listt;
int temp;
while (cin >> temp)
listt.push_back(temp);
for (auto beg = listt.cbegin(), end = listt.cend(); beg != end; beg++)
{
if (*beg % 2)
deq_first.push_back(*beg);
else
deq_second.push_back(*beg);
}
for (auto c : deq_first)
cout << c << ends;
cout << endl;
for (auto c : deq_second)
cout << c << ends;
cout << endl;
}
9.21 会移动插入位置之后的所有元素,费时费力
9.22 迭代器失效
9.23 都一样
9.24
int main(int argc, char* argv[])
{
vector<int> vectemp;
cout << vectemp.at(1) << ends;
cout << vectemp[2] << ends;
cout << vectemp.front() << ends;
cout << *vectemp.begin() << endl;
return 0;
}
提示错误,程序终止
9.25 不会发生啥
9.26
int main(int argc, char* argv[])
{
int ia[] = { 0,1,1,2,3,5,8,13,21,55,89 };
vector<int> vectemp;
list<int> listtemp;
vectemp.assign(ia, ia + 11);
listtemp.assign(ia, ia + 11);
for (auto beg = vectemp.begin(); beg != vectemp.end(); )
{
if (*beg % 2 == 0) beg = vectemp.erase(beg);
else ++beg;
}
for (auto beg = listtemp.begin(); beg != listtemp.end();)
{
if (*beg % 2) beg = listtemp.erase(beg);
else ++beg;
}
cout << "删除偶数元素后" << endl;
for (auto c : vectemp)
cout << c << ends;
cout << endl;
cout << "删除奇数元素后" << endl;
for (auto c : listtemp)
cout << c << ends;
cout << endl;
return 0;
}
9.27
#include <iostream>
#include <deque>
#include <list>
#include <forward_list>
using namespace std;
int main()
{
int temp;
vector<int> vv;
while (cin >> temp)
vv.push_back(temp);
forward_list<int> ff(vv.cbegin(), vv.cend());
auto befor = ff.before_begin();
auto now = ff.begin();
for (; now != ff.end(); ++befor)
{
if (*now % 2)
now = ff.erase_after(befor);
else
++now;
}
for (auto c : ff)
cout << c << ends;
cout << endl;
}
9.28
void stupid_insert(forward_list<string> &fortemp, string a, string b)
{
auto prev = fortemp.before_begin();
auto curr = fortemp.begin();
auto end = fortemp.cend();
for (; curr != end; ++curr, ++prev)
{
if (*curr == a)
{
fortemp.insert_after(curr, b);
return;
}
}
fortemp.insert_after(prev, b);
return;
}
9.29 调整为100个元素,新增元素进行值初始化
删除后边90个元素
9.30 有默认构造函数
9.31 不能,forward_list没有insert函数
list和forward_list迭代器不支持加减法
9.32 不合法。程序逻辑混乱
9.33 迭代器失效
9.34 ++iter不在while循环中,插入新元素位置不对
9.35 capacity是最大能容纳的元素数量,size是现有元素数量
9.36 不可能
9.37 array数量固定list不连续存储
9.38 略
9.39 创建一个vec,分配最少1024个元素所占的内存,读取元素,加入vec中,再增加已有元素数量的一半
9.40 ?
9.41
int main(int argc, char* argv[])
{
vector<char> vectemp{'a','b','c','d','e','f','g','h'};
string s(vectemp.begin(), vectemp.end());
string t(vectemp.data(), vectemp.size());
cout << s << endl;
cout << t << endl;
return 0;
}
9.42 先分配一个足够大的内存,再逐个读入(同vector)
9.43-9.44(答案用find成员函数,本节没讲)
void stupid_replace(string &aim, const string &a, const string &b)
{
string::size_type i;
for (i = 0; i != aim.size(); ++i)
{
if (aim.substr(i, a.size()) == a)
{
aim.replace(i, a.size(), b);
i += b.size();
}
}
return;
}