C++ Primer问题总结(6)

第9章

问题

1.string和vector元素是存储在连续的内存空间中,因此删除插入元素会导致多个元素的移动。另外因为连续,可以通过下标来随机访问元素。
2.list的元素存储不在连续空间,因此不能随机访问元素,但是增删很快,改查很慢。
3.deque双端队列,支持快速随机访问元素,增删也较快
4.array是固定大小数组,forward_list是单向的,它不能push_back()、size()。为了获得一个forward_list对象的大小,可以使用distance算法来计算begin和end之间的距离,这个操作是线性时间复杂度的。
5.容器的大小用size_type衡量
6.顺序容器复制构造函数参数类型必须与调用对象同类型,如果构造函数参数是迭代器,那么类型可以不同,但是容器内元素的类型要可以相互转换。比如int和double
7.array不允许花括号列表赋值,这里不是列表初始化
8.swap两个参数的类型必须相同,因为元素并没有移动,所以迭代器不会失效(string除外)
9.因为array是固定大小,所以用swap交换array对象,会真正交换他们的元素。
10.insert(p,t)在p指向元素之前创建一个t
11.当用一个对象来初始化容器时,或将一个对象插入到容器中时,实际上放入容器中的是对象值的一个拷贝。
12.push.back() array和forward_list不支持
13.push.front() string和vector不支持,每次插入都需要移动后面的元素,能支持才怪了。
14.insert可以支持在容器内任意位置插入元素。我估计这个成员函数是之后才加加上去的。
15.需要注意的是insert(p,args)返回的是一个迭代器,该迭代器指向的是第一个新加入元素,如果没有元素插入,则返回p。
16.emplace和insert的区别,个人认为只是传递对象和临时对象(调用构造函数)的区别。emplace一次只能传递一个对象,insert可以传递对象的一个范围。
17.每个顺序容器都有一个front成员函数,除了forward_list外,每个顺序容器都有一个back成员函数
18.vector、deque、string和aray类型可以直接使用下标和at[]访问元素。
19.c.at(n)返回下标为n的元素的引用,如果下标越界,则抛出异常。
20.erase和insert一样,会返回一个迭代器,erase返回一个指向删除元素之后元素的迭代器。insert返回一个指向新插入元素的迭代器
21.forward_list是单向链表,所以新增删除元素最好是在一个元素之后,有insert_after、emplace_after、erase_after。
22.resize改变容器大小,array不支持,如果当前大小大于所要求的大小,则删除容器后部的元素;如果当前大小小于所要求的大小,则往容器后部新增元素,原有的元素不改变。
23.shrink_to_fit()将capacity()减少为size()大小
24.string类的substr()中的参数是字符串下标
25.string类支持insert和serase,但是其中的参数不一定是迭代器,可以是下标类型
26.string有replace和append成员函数,replace代替,append拼接
27.string搜索操作,每个搜索操作都返回一个下标
find、find_first_of、find_last_of、find_first_not_of、find_last_not_of、rfind

习题

9.2

/*9.2*/
	deque<int>a = { 1,2,3 };
	deque<int>b(a.begin(),a.end());
	list<deque<int>> l;
	l.insert(l.begin(),10, a);
	for (auto i : l)
	{
		for (auto a : i)
			cout << a << endl;
	}
	cout << *(l.begin()) << *(l.end());

9.4

bool it(vector<int>::iterator i, vector<int>::iterator j, int k)
{
	for (auto a = i; a != j; a++)
	{
		if (*a == k)
			return true;
	}
	return false;
}
int main()
{	
	/*9.4*/
	vector<int>i{ 1,2,3 };
	cout << it(i.begin(), i.end(), 2);

}

9.5

vector<int>::iterator it(vector<int>::iterator i, vector<int>::iterator j, int k)
{
	for (auto a = i; a != j; a++)
	{
		if (*a == k)
			return a;
	}
	return j;
}
int main()
{	
	/*9.5*/
	vector<int>i{ 1,2,3 };
	cout << *(it(i.begin(), i.end(), 2));
}

9.13

/*9.13*/
#include <iostream>
#include <string>
#include <vector>
#include <list>

using std::list;
using std::vector;
using std::cout;
using std::endl;

int main()
{
    list<int> ilst(5, 4);
    vector<int> ivc(5, 5);

    //! from list<int> to vector<double>
    vector<double> dvc(ilst.begin(), ilst.end());
    for (auto i : ilst) cout << i;
    cout << endl;
    for (auto t : dvc) cout << t;
    cout << endl;

    //! from vector<int> to vector<double>
    vector<double> dvc2(ivc.begin(), ivc.end());
    for (auto i : ivc) cout << i;
    cout << endl;
    for (auto t : dvc2) cout << t;

    return 0;
}

9.14

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

9.15

bool Compare(vector<int> a, vector<int> b)
{
	if (a > b)
		return true;
	else
		return false;
}
int main()
{
/*9.15*/
	vector<int> a, b;	//默认初始化
	cout << Compare(a, b) << endl;	//结果返回o
}

9.16

int main()
{
/*9.16*/
	std::list<int> list{ 1, 2, 3, 4, 5 };
	std::vector<int> vec1{ 1, 2, 3, 4, 5 };
	std::vector<int> vec2{ 1, 2, 3, 4 };
/*用字符形式输出*/
	std::cout << std::boolalpha
		<< (std::vector<int>(list.begin(), list.end()) == vec1)
		<< std::endl;
	std::cout << std::boolalpha
		<< (std::vector<int>(list.begin(), list.end()) == vec2)
		<< std::endl;
}

9.18

/*9.18*/
int main()
{
	string a;
	deque<string> d;
	while (cin >> a)
	{
		d.push_back(a);
		if (a == "^Z")
			break;
	}
	for (auto p : d)
		cout << p << endl;

}

9.20

int main()
{
	/*9.20*/
	list<int>a = { 1,2,3,4,5,6,7,8,9 };
	deque<int>b, c;
	for (auto p = a.begin(); p != a.end(); p++)
	{
		if ((*p) % 2 == 0)
			b.push_front(*p);
		else
			c.push_front(*p);
	}
	for (auto p : b)
		cout << p << endl;
	for (auto p : c)
		cout << p << endl;
}

9.24

int main()
{
	/*9.24*/
	vector<int>v{1};
	int v1 = v.front();
	int v2 = (*v.begin());
	int v3 = v[0];
	int v4 = v.at(0);
	cout << v1 << v2 << v3 << v4 << endl;
}

9.26

void show(vector<int>i,list<int> l,int p[])
{
	int a = 0;
	while(a<=11)
	{
		auto it = (l.begin())++;
		auto it2 = (i.begin())++;
		if((p[a]%2)==1 && it!=l.end())
			l.erase(it);
		else
			i.erase(it2);
		a++;
	}
}
int main()
{
	/*9.26*/
	int ia[] = { 0,1,1,2,3,5,8,13,21,55,89 };
	vector<int> v(ia,ia+11);
	list<int>l(ia,ia+11);
	show(v, l, ia);
	for (auto b : v)
		cout << b ;
	for (auto b : l)
		cout << b;

}

9.27

int main()
{
/*9.27*/
    forward_list<int> flst = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    for (auto prev = flst.before_begin(), curr = flst.begin();
         curr != flst.end();)
        if (*curr & 0x1)
            curr = flst.erase_after(prev);
        else
            prev = curr++;

    for (auto i : flst) cout << i << " ";
    cout << endl;
}

9.28

void Search(forward_list<string> &fl, const string &a, const string &b)
{
	auto p1 = fl.before_begin();
	for(auto p = fl.begin();p!=fl.end();p1++,p++)
	{
		if (*p == a)
			fl.insert_after(p, b);
		return;
	}
	fl.insert_after(p1,b);
}
int main()
{
	/*9.28*/
	forward_list<string> FL{"hello"};
	Search(FL, "hello", "world");
	for (auto p : FL)
		cout << p << endl;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值