关于size,capacity和reserve

hww19898318 2010-03-26 12:41:20
小弟对于这个搞的不是很懂,特别是capacity和reserve,,另外再想问问关于适配器的内容
先谢了
...全文
153 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
FingerStyle 2010-03-26
  • 打赏
  • 举报
回复
适配器就是改变下接口的样子而已。

想STL里 一些容器如stack等 是用的已经有的容器,只是改变一下接口来表示适合自己特性的方法

例如 stack<int, list<int> > sta;

以list<int> 作为容器
昵称很不好取 2010-03-26
  • 打赏
  • 举报
回复
之所以有capacity和reserve,是为了防止插入数据频繁复制所造成的性能损耗,假设你本来的容器vector存储在内存为32k的一块内存区域,当插入的数据多于32k的时候,该容器就得寻找一个更大的空间以方便你继续插入数据,并把原来32k的内存空间数据复制到新的空间,这些复制便造成内存损耗。
于是可以在插入数据之前调用reserve来为容器寻找一个更大的内存空间,直接在这个空间插入数据,这个空间的大小取决于你所设置的reserve参数
yuzl32 2010-03-26
  • 打赏
  • 举报
回复
size 代表容器的元素个数
太乙 2010-03-26
  • 打赏
  • 举报
回复
void reserve ( size_type n );

Request a change in capacity

Requests that the capacity of the allocated storage space for the elements of the vector container be at least enough to hold n elements.

This informs the vector of a planned change in size, which can be either to expand or shrink its storage capacity, although notice that the parameter n informs of a minimum, so the resulting capacity may be any capacity equal or larger than this.

When n supposes an expansion in capacity, a reallocation happens during the call to this function, granting that no further automatic reallocations will happen because of a call to vector::insert or vector::push_back until the vector size surpasses at least n (this preserves the validity of iterators on these calls).

A reallocation invalidates all previously obtained iterators, references and pointers.

In any case, a call to this function never affects the elements contained in the vector, nor the vector size (for that purposes, see vector::resize or vector::erase, which modify the vector size and content).
太乙 2010-03-26
  • 打赏
  • 举报
回复
size_type capacity () const;  

Return size of allocated storage capacity

Returns the size of the allocated storage space for the elements of the vector container.
昵称很不好取 2010-03-26
  • 打赏
  • 举报
回复
capacity表示为容器预留多少空间
而reserve可以设置这个空间的大小
GoForSky 2010-03-26
  • 打赏
  • 举报
回复
适配器
就是把容器或迭代器的接口改变一下
ShineShineRedStar 2010-03-26
  • 打赏
  • 举报
回复
可以看一下书里的介绍:
http://book.csdn.net/bookfiles/18/10018635.shtml
cy330206 2010-03-26
  • 打赏
  • 举报
回复
新手容易别误导的啊,,呵呵
cy330206 2010-03-26
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 thefirstz 的回复:]
引用 7 楼 cy330206 的回复:

引用 1 楼 thefirstz 的回复:
capacity表示为容器预留多少空间
而reserve可以设置这个空间的大小


貌似这个反了。。

好像意思确实没表达好~~
但我的下一句reserve可以消除疑问啊
[/Quote]

.............
昵称很不好取 2010-03-26
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 cy330206 的回复:]

引用 1 楼 thefirstz 的回复:
capacity表示为容器预留多少空间
而reserve可以设置这个空间的大小


貌似这个反了。。
[/Quote]
好像意思确实没表达好~~
但我的下一句reserve可以消除疑问啊
cattycat 2010-03-26
  • 打赏
  • 举报
回复
size表示当前大小,capacity表示总的容量,reverse表示反转容器。
zhongjilei 2010-03-26
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 thefirstz 的回复:]

capacity表示为容器预留多少空间
而reserve可以设置这个空间的大小
[/Quote]
reserve是给容器预分配空间
而capacity是给容器分配了多少空间
size是容器中存储数据的个数

如:
例1:
vector<int> vecInt;
vecInt.reserve(20);
int n = vecInt.capacity();
int m = vecInt.size();
m的值为0
n的值为20
例2:
vector<int> vecInt;
vecInt.push_back(1);
int m = vecInt.size();
int n = vecInt.capacity();
m的值为1
n的值为1
cy330206 2010-03-26
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 thefirstz 的回复:]
capacity表示为容器预留多少空间
而reserve可以设置这个空间的大小
[/Quote]

貌似这个反了。。

64,646

社区成员

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

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