C++ Primer(5e)第9章习题

9.1

(a)vector
(b)deque
(c)vector

9.2

list<deque<int>>

9.3

1.它们指向同一个容器中的元素,或者是容器最后一个元素之后的位置。
2.我们可以通过反复递增begin来达到end。

9.4

bool find(vector<int>::iterator beg, vector<int>::iterator end, int n)
{
	while (beg != end)
	{
		if (*beg == n)
			return true;
		++beg;
	}
	return false;
}

9.5

int find(vector<int>::iterator beg, vector<int>::iterator end, int n)
{
	while (beg != end)
	{
		if (*beg == n)
			return n;
		++beg;
	}
	return end;
}

9.6

无序关联容器list不支持关系运算符;修改: 将< 改为 !=

9.7

vector<int>::iterator

9.8

list<string>::value_type, list<string>::reference

9.9

begin被重载过,cbegin没有。

9.10

vector<int> v1;   
const vector<int> v2;  
auto it1 = v1.begin(), it2 = v2.begin();   //普通  const
auto it3 = v1.cbegin(), it4 = v2.cbegin();  // const  const

9.11

vector<int> a;
vector<int> a(b);
vector<int> a = b;
vector<int> a = {1, 2, 3};
vector<int> a{1, 2, 3};
vector<int> a(1,3);
vector<int> a(3);
vector<int> a(b.begin(), b.end());

9.12

为了创建一个容器为另一个容器的拷贝,两个容器的类型及其元素类型必须匹配;不过,当传递迭代器参数来拷贝一个范围时,就不要求容器类型是相同的了。

9.13

#include
#include
#include

using namespace std;

int main()
{
	list<int> lst = { 1, 2, 3 };
	vector<int> a = { 1, 2, 3 };
	vector<double> num(lst.begin(), lst.end());
	vector<double> b(a.begin(), a.end());
	return 0;
}

9.14

#include
#include
#include
#include

using namespace std;

int main()
{
	list<const char *> a = { "a", "b", "c" };
	vector<string> b;
	b.assign(a.begin(), a.end());
	for (auto &c : b)
		cout << c << endl;
	return 0;
}

9.15

bool IsEqual(const vector<int>& a, const vector<int>& b)
{
	if (a.size() != b.size())
		return false;
	for (int i = 0; i != a.size(); ++i)
	{
		if (a.size() != b.size())
			return false;
	}
	return true;
}

9.16

bool IsEqual(const list<int>& a, const vector<int>& b)
{
	vector<int> c(a.begin(), a.end());
	if (c.size() != b.size())
		return false;
	for (int i = 0; i != c.size(); ++i)
	{
		if (c.size() != b.size())
			return false;
	}
	return true;
}

9.17

c1和c2的类型相同并且支持关系运算符<

9.18

#include
#include
#include

using namespace std;

int main()
{
	string word;
	deque<string> a;
	while (cin >> word)
		a.push_back(word);
	for (auto it = a.begin(); it != a.end(); ++it)
		cout << *it << endl;
	return 0;
}

9.19

#include
#include
#include

using namespace std;

int main()
{
	string word;
	list<string> a;
	while (cin >> word)
		a.push_back(word);
	for (auto it = a.begin(); it != a.end(); ++it)
		cout << *it << endl;
	return 0;
}

9.20

#include
#include
#include

using namespace std;

int main()
{
	list<int> a = { 1, 2, 3, 4, 5, 6 };
	deque<int> b1, b2;
	for (auto it = a.begin(); it != a.end(); ++it)
	{
		if (*it % 2 == 0)
			b1.push_back(*it);
		else
			b2.push_back(*it);
	}
	for (auto iter1 = b1.begin(); iter1 != b1.end(); ++iter1)
		cout << *iter1 << " ";
	cout << endl;
	for (auto iter2 = b2.begin(); iter2 != b2.end(); ++iter2)
		cout << *iter2 << " ";
	cout << endl;
	return 0;
}

9.21

每次循环迭代器会往后移一个元素位置

/* 程序测试*/
#include
#include
#include

using namespace std;

int main()
{
	vector<string> lst;
	auto iter = lst.begin();
	string word;
	while (cin >> word)
		iter = lst.insert(iter, word);
	for (auto it = lst.begin(); it != lst.end(); ++it)
		cout << *it << endl;
	return 0;
}

9.22

mid迭代器失效

#include
#include

using namespace std;

int main()
{
	vector<int> iv = { 1, 2, 5, 5, 6, 3 };
	vector<int>::iterator iter = iv.begin(),
		mid = iv.begin() + iv.size() / 2;
	int some_val = 5;
	while (iter != mid)
	{
		if (*iter == some_val)
		{
			iter = iv.insert(iter, 2 * some_val);
			mid = iv.begin() + iv.size() / 2 + 1;
			iter += 2;
		}
		else
			iter++;
	}
	for (auto it : iv)
		cout << it << " ";
	cout << endl;
	return 0;
}

9.23

它们的值都是容器中唯一的元素

9.24

/* 当为空的时候会出错 */
#include
#include

using namespace std;

int main()
{
	vector<int> c;
	auto a = c.at(0);
	auto b = c[0];
	auto d = c.front();
	auto e = c.begin();
	cout << a << " " << b << " " << d << " "
		<< e << endl;
	return 0;
}

9.25

如果elem1与elem2相等则不会删除元素;如果elem2为尾后迭代器,则会删除elem1直至最后一个元素;如果elem1与elem2都为尾后迭代器,则不会删除元素.

9.26

#include
#include
#include

using namespace std;

int main()
{
	int ia[] = { 0, 1, 2, 3, 4, 5, 8, 13, 21, 55, 89 };
	vector<int> a;
	list<int> b;
	for (int i = 0; i < 11; ++i)
	{
		a.push_back(ia[i]);
		b.push_back(ia[i]);
	}
	auto it1 = a.begin();
	while (it1 != a.end())  // 删除偶数元素
	{
		if (*it1 % 2 == 0)
			it1 = a.erase(it1);
		else
			++it1;
	}
	auto it2 = b.begin();
	while (it2 != b.end())  // 删除奇数元素
	{
		if (*it2 % 2)
			it2 = b.erase(it2);
		else
			++it2;
	}
	for (auto c : a)
		cout << c << " ";
	cout << endl;
	for (auto c : b)
		cout << c << " ";
	cout << endl;
	return 0;

9.29

vec.resize(100) 会将75个值为0的元素添加到vec末尾;
vec.resize(10) 会删除后面90个元素.

9.30

如果容器保存的是类类型,必须提供初始值或者元素类型必须提供一个默认构造函数。

9.31

list和forward_list的迭代器不支持复合运算 iter += 2;
修改: ++iter; ++iter;

9.32

不合法,后置递增运算符++的优先级高于解引用*

9.33

程序引发异常崩溃

9.34

死循环

#include
#include

using namespace std;

int main()
{
	vector<int> vi = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
	auto iter = vi.begin();
	while (iter != vi.end())
	{
		if (*iter % 2)   
			iter = vi.insert(iter, *iter);
		++iter;
	}
	for (auto c : vi)
		cout << c << " ";
	cout << endl;
	return 0;
}

9.35

容器的size是指它已经保存的元素的数目;而capacity则是在不分配新的内存空间的前提下它最多可以保存多少元素。

9.36

不可能

9.37

list不需要事先分配内存空间,array不允许改变容器的大小。

9.38

#include
#include

using namespace std;

int main()
{
	vector<int> ivec;
	cout << "ivec: size: " << ivec.size()
		<< " capacity : " << ivec.capacity() << endl;
	for (vector<int>::size_type ix = 0; ix != 24; ++ix)
		ivec.push_back(ix);
	cout << "ivec: size: " << ivec.size()
		<< " capacity: " << ivec.capacity() << endl;
	ivec.reserve(50);
	cout << "ivec: size: " << ivec.size()
		<< " capacity: " << ivec.capacity() << endl;
	while (ivec.size() != ivec.capacity())
		ivec.push_back(0);
	cout << "ivec: size: " << ivec.size()
		<< " capacity: " << ivec.capacity() << endl;
	ivec.push_back(42);
	cout << "ivec: size: " << ivec.size()
		<< " capacity: " << ivec.capacity() << endl;
	ivec.shrink_to_fit();
	cout << "ivec: size: " << ivec.size()
		<< " capacity: " << ivec.capacity() << endl;
	return 0;
}

9.39

vector<string> svec;
svec.reserve(1024);   // 给svec分配1024个元素的空间
string word;
while (cin >> word)
	svec.push_back(word);
svec.reserve(svec.size() + svec.size() / 2);  // 将容器容量扩大为1.5倍

9.40

如果读入256, 512, 1000, 1024个词,capacity都为1024 * 1.5 = 1536

9.41

vector<char> v = {'h', 'l', 'l', 'e', 'o'};
string s(v.begin(), v.end());

9.42

使用reserve(100)

你可能感兴趣的:(C++)