c++ Primer的问题,牛牛们给个指点。

kunlun64 2006-09-03 09:54:46
第11章 泛型算法里
11.7 判断下面程序是否有错,如果有请改正。
(b) vector<int> vec;
vec.reserve(10);
fill_n(vec.begin(),10,0);

11.8 前面说过,算法不改变它所操纵的容器大小,为什么使用back_inserter也不能突破这个限制。

想不明白,back_inserter不是相当于调用push_back吗?前面讲解里例子有:
vector<int> vec;
fill_n(back_inserter(vec),10,0);
这样不是改变了容器大小吗?
请牛牛们给指点一下。
...全文
433 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
OOPhaisky 2006-09-03
  • 打赏
  • 举报
回复
说实话,没太看懂楼主的问题,但是

vector<int> vec;
fill_n(back_inserter(vec),10,0);
这样不是改变了容器大小吗?
---------------------------------------
这样的确改变了容器的大小(size),这是没错的。


vector<int> vec;
vec.reserve(10);
fill_n(vec.begin(),10,0);
---------------------------------
这是错误的,因为reserve只是改变了capacity,没有改变size,容器中并没有‘有效’元素。
kunlun64 2006-09-03
  • 打赏
  • 举报
回复
谢谢,steedhorse(晨星)版主,对关键概念上重新获得理解。
结贴。
晨星 2006-09-03
  • 打赏
  • 举报
回复
没有问题啊,Lippman前面自己不已经给出这个问题的答案了吗?

Key Concept: Algorithms Never Execute Container Operations
The generic algorithms do not themselves execute container operations. They operate solely in terms of iterators and iterator operations. The fact that the algorithms operate in terms of iterators and not container operations has a perhaps surprising but essential implication: When used on "ordinary" iterators, algorithms never change the size of the underlying container. As we'll see, algorithms may change the values of the elements stored in the container, and they may move elements around within the container. They do not, however, ever add or remove elements directly.

As we'll see in Section 11.3.1 (p. 406), there is a special class of iterator, the inserters, that do more than traverse the sequence to which they are bound. When we assign to these iterators, they execute insert operations on the underlying container. When an algorithm operates on one of these iterators, the iterator may have the effect of adding elements to the container. The algorithm itself, however, never does so.


fill_n(back_inserter(vec),10,0);
中,问题在于,fill_n算法从来都不会改变容器的size,改变容器size的是back_inserter,back_inserter并不是一种“算法”,它可以看成是种“迭代适配器”。
于是,上面这条语句中的fill_n是一直是在“被back_inserter修改着的”容器中插入无素,它本身从来不曾修改vec容器的大小,而且也不知道容器是否被谁改过。
换句话说,它只会低着头不断往里插,却不曾知道back_inserter一直在背地里协助它,并为之承担了“修改容器大小”的骂名,呵呵。
kunlun64 2006-09-03
  • 打赏
  • 举报
回复
to OOPhaisky(渴望成功)
这样的确改变了容器的大小(size),这是没错的。
=============================================
是呀,我也认为是改变了容器大小。可大师Lippman在书上说没有。难道是翻译的问题?谁有原版给查查,11.8题是不是这个意思。

还有哪位大牛来指点一下。
jixingzhong 2006-09-03
  • 打赏
  • 举报
回复
第一个问题也是这个原因,
vec.reserve(10); 只是增加了 容量,
里面的元素空间仍是 0 ...

fill_n 需要有效元素空间...
jixingzhong 2006-09-03
  • 打赏
  • 举报
回复
我的意思就是,
你要把这个 容器的容量 和容器的元素个数 区别开来 ...

插入只是增加元素, 不能增加容量 ...
(这个好像就是你的问题了 晕 =_= 楼主你就当作一个概念记住吧,用多了就明白了)
jixingzhong 2006-09-03
  • 打赏
  • 举报
回复
vec.reserve(10);
fill_n(vec.begin(),10,0);
=============
reserve 只是设置了 capacity,
还是没有元素空间的 ...
kunlun64 2006-09-03
  • 打赏
  • 举报
回复
to jixingzhong(瞌睡虫:选择了远方,只顾风雨兼程!)
谢谢你的帮助。
可vector<int> vec定义的是空容器呀。vec.size()和vec.capacity()都等于0,这在编译里测试过的。而且容器预留分配也是根据现有大小翻倍分配的,不能简单理解为100吧。
jixingzhong 2006-09-03
  • 打赏
  • 举报
回复
11.8 前面说过,算法不改变它所操纵的容器大小,为什么使用back_inserter也不能突破这个限制。
=======
你可以这么理解,
容器一次性给出了若干个空间, 比如可以放 100 个元素,
但是你只放了 10 个有效元素,
然后 back_inserter 只是在这100个空间中插入有效元素,
并不改变这个容器空间(100)
lann64 2006-09-03
  • 打赏
  • 举报
回复
mark
kunlun64 2006-09-03
  • 打赏
  • 举报
回复
昏,越来越麻烦。
我的测试程序没写#include <algorithm>怎么也通过编译了。
kunlun64 2006-09-03
  • 打赏
  • 举报
回复
难道说Lippman大师出了错,或者gcc实现没遵守标准?
kunlun64 2006-09-03
  • 打赏
  • 举报
回复
谢谢各位。
第一个问题明白了,fill_n(vec.begin(),10,0)并不能改变vec.size().
但第二个问题还是不懂。我用编译器测试,发现容器大小的确是改变了。
#include <iostream>
#include <vector>

using namespace std;
int main()
{
vector<int> vec;
cout << "size: " << vec.size() << " capacity: " << vec.capacity() << endl;
fill_n(back_inserter(vec),10,0);
cout << "size: " << vec.size() << " capacity: " << vec.capacity() << endl;

return 0;
}
输出结果:
size: 0 capacity:0
size: 10 capacity: 16

我用的是minGW

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧