9.1
(a) list , 因为需要中间插入
(b) deque , 因为需要在头尾进行插入与删除操作
(c) vector , 因为只需要从尾部插入,再排序即可
9.2
list
9.3
1.指向同一个容器
2.begin <= end
9.4
bool findInt(vector::iterator begin, vector::iterator &end, int &value)
{
while(begin != end)
{
if(*begin == value)
return true;
begin++;
}
return false;
}
9.5
vector::iterator findInt(vector::iterator begin, vector::iterator &end, int &value)
{
while(begin != end)
{
if(*begin == value)
return begin;
begin++;
}
return end;
}
9.6
while(iter1 != iter2)
9.7
vector
9.8
读取:list
写入:list
9.9
cbegin 指定的对象只读
9.10
it1: vector
it2: vector
it3: vector
it4: vector
9.11
vector
vector
vector
vector
vector
vector
9.12
第一种类型需要一致,而且是全部拷贝
第二种类型不需要一致,可以选择拷贝范围
9.13
vector
vector
9.14
vector
vecString.assign(list.cbegin(), list.cend());
9.16
因为它们类型不同,所以先把list初始化成vector后再比较
vector
9.17
都是顺序容器,且定义了比较操作
9.18
#include
#include
#include
using namespace std;
int main(int argc, char *argv[])
{
string s;
deque deque1;
while(cin>>s)
{
if(s == "break")
break; //我实在不知道怎么退出了,command+d没用
deque1.push_back(s);
}
for(auto &value :deque1)
cout<< value << endl;
return 0;
}
9.19
感觉没啥区别
#include
#include
#include
using namespace std;
int main(int argc, char *argv[])
{
string s;
list list1;
while(cin>>s)
{
if(s == "break")
break; //我实在不知道怎么退出了,command+d没用
list1.push_back(s);
}
for(auto &value :list1)
cout<< value << endl;
return 0;
}
9.20
#include
#include
#include
#include
using namespace std;
int main(int argc, char *argv[])
{
list list1 ={12,3,4,5,32,6,3,33,11};
deque deque1, deque2;//deque1存偶数,deque2存奇数
for(auto &value :list1)
{
if(value%2 == 0)
deque1.push_back(value);
else
deque2.push_back(value);
}
for(auto &value : deque1)
cout << value << " ";
cout << endl;
for(auto &value : deque2)
cout << value << " ";
cout << endl;
return 0;
}
9.21
一样效果,记住insert()的返回值 恰好是指向新元素的迭代器
9.22
#include
#include
#include
using namespace std;
int main(int argc, char *argv[])
{
vector iv ={12,5,3,5,32,5,5,3,5,11,44,4,22};
int some_value =5;
auto cursor = iv.size() / 2;
auto iter = iv.begin(), mid = iv.begin() + cursor;
while (iter != mid)
{
if(*iter == some_value)
{
iter = iv.insert(iter, 2 * some_value); //必须更新iter,因为iter已经失效
iter++; //iter指向新元素,所以要++
mid = iv.begin() + ++cursor;
}
iter++;
}
for(auto &value :iv)
cout << value << " ";
cout << endl;
return 0;
}
9.23
4个值都等于第一个元素的值
9.24
vector
cout << v.at(0) << endl; //libc++abi.dylib: terminating with uncaught exception of type std::out_of_range: vector
cout << v[0] << endl;
cout << v.front() <
9.25
如果elme1 与 elme2 相等,不删除东西
如果elme2 是尾后迭代器,则删除elme1到结尾的元素
如果都是尾后迭代器,不删除东西
9.26
#include
#include
#include
using namespace std;
int main(int argc, char *argv[])
{
int ia[] = {0, 1, 1, 2, 3, 5, 8, 13, 21, 55, 89};
vector vec(ia, end(ia));
list lst(ia, end(ia));
auto vecIt = vec.begin();
while (vecIt != vec.end())
{
if(*vecIt %2 == 0)
vecIt = vec.erase(vecIt);
else
vecIt++;
}
auto lstIt = lst.begin();
while (lstIt != lst.end())
{
if(*lstIt %2 != 0)
lstIt = lst.erase(lstIt);
else
lstIt++;
}
for(auto i : vec)
cout << i << " ";
cout << endl;
for(auto i : lst)
cout << i << " ";
cout << endl;
return 0;
}
9.28
#include
#include
#include
using namespace std;
void insert(forward_list &lst, const string &a, const string &b)
{
auto prev = lst.before_begin();
auto it = lst.begin();
while(it != lst.end())
{
if(*it == a)
{
lst.insert_after(it, b);
return;
}
else
prev = it++;
}
lst.insert_after(prev, b); //prev的作用体现在,插入只能在尾迭器前一个
}
int main(int argc, char *argv[])
{
forward_list lst = {"cat", "dog", "apple", "pig"};
string a = "apple";
string b = "bird";
insert(lst, a, b);
for(auto &i : lst)
cout << i << " ";
cout << endl;
return 0;
}
9.29
(1)添加75个值为0的元素
(2)删除后面90个元素9.31
list: iter += 2; 改为 advance(iter, 2);
fordward_list:
#include
#include
using namespace std;
int main(int argc, char *argv[]) {
forward_list lst = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
auto iter = lst.begin();
auto prev = lst.before_begin();
while(iter != lst.end())
{
if(*iter %2) //复制奇数
{
iter = lst.insert_after(iter, *iter);
prev = iter++;
}
else //删除偶数
iter = lst.erase_after(prev);
}
for(auto i : lst)
cout << i << " ";
cout << endl;
return 0;
}
9.32
不合法,因为执行插入操作后,iter已经失效,iter++未知
9.33
执行插入操作后,begin失效
9.34
无限循环,把++iter 改为 advance(iter, 2);
9.35
size 是存储的元素的个数
capacity 是分配的存储空间个数
9.36
不能
9.37
list 是链表,数据不是连续存储的
array 是固定大小的
9.38 输出: 9.40 9.41 9.43 9.44 9.45 9.46 9.47 9.48 9.49 9.50 9.51 mytime.cpp main.cpp 9.52
vector
cout << vec.size() <
cout << vec.size() <
10
10
11
20
可以发现capacity *2 了
数太多了没试,反正读入1000与1048个词时capacity会增加
string s(vec.begin(), vec.end());
//迭代器实现void replace(string &s, const string &oldVal, const string &newVal)
{
auto it = s.begin();
while(distance(it, s.end()) >= distance(oldVal.begin(), oldVal.end()))
{
if(string(it, it + oldVal.size()) == oldVal)
{
it = s.erase(it, it + oldVal.size());
it = s.insert(it, newVal.begin(), newVal.end());
advance(it, newVal.size());
}
it++;
}
}
//下标实现void replace(string &s, const string &oldVal, const string &newVal)
{
string::size_type i =0;
while(( s.size() - i) >= oldVal.size())
{
if(s.substr(i, oldVal.size()) == oldVal)
{
s.replace(i,oldVal.size(), newVal);
i = i + newVal.size();
}
else
i++;
}
}
string fun(string name, const string &s1, const string &s2)
{
name.insert(name.begin(), s1.begin(),s1.end()); //3个参量都应该是迭代器
name.append(s2);
return name;
}
string fun(string name, const string &s1, const string &s2)
{
name.insert(0, s1);
name.insert(name.size(), s2);
return name;
}
#include
string::npos#include
#include
mytime.h#ifndef PRO1_MYTIME_H
#define PRO1_MYTIME_H
#include
#include "mytime.h"
mytime::mytime(const string &s)
{
const string delimiters= " /,";
auto posmonth = s.find_first_of(delimiters);
month = nameToMonth(s.substr(0, posmonth));
auto posday = s.find_first_of(delimiters, posmonth+1);
day = (unsigned)stoi(s.substr(posmonth+1, posday-posmonth-1));
year = (unsigned)stoi(s.substr(posday+1));
if( !(day<=31 && day>=0 && month >=1 && month <=12) )
throw invalid_argument("Error!!!!");
}
unsigned mytime::nameToMonth(const string &s)
{
if(s.empty())
return 0;
if(isdigit(s[0]))
return (unsigned)stoi(s);
for(int i =0; i< 12; i++)
{
if(s.find(month_names[i]) != string::npos )
return (unsigned)i+1;
}
return 0;
}
void mytime::print()
{
cout << "year: " << year;
cout << " month: " << month;
cout << " day: "<< day << endl;
}
#include
#include