public member function
<vector>
std::vector::reserve
void reserve (size_type n);
Request a change in capacity
Request that thevector capacitybe at least enough to containnelements.
要求vector的容量至少能够容纳n个元素。
Ifnis greater than the currentvector capacity, the function causes the container to reallocate
its storage increasing itscapacityton(or greater).
如果n比现在的capacity大,那么这个函数会使容器重新分配其空间使其容量达到n(或者更大)
In all other cases, the function call does not cause a reallocation and thevector capacityis not affected.
在其他情况下,该方法不会导致重分配的情况发生,并且其容量不会受到影响。
This function has no effect on thevector sizeand cannot alter its elements.
该方法不会影响数组的大小,也不会修改其元素。
Parameters
n
Minimumcapacityfor thevector.
Note that the resultingvector capacitymay be equal or greater thann.
Member typesize_typeis an unsigned integral type.
参数:
n
vector的最小容量。
要注意capacity可能等于也可能大于n
n为一个无符号整型
Return Value
none
无返回值
If the size requested is greater than the maximum size (vector::max_size), alength_errorexception
is thrown.
如果要求的大小比max_size获得的值更大,那么将会抛出一个length_error异常.
If case of reallocation, the storage is allocated using the container'sallocator, which may throw
exceptions on failure (for the defaultallocator,bad_allocis thrown if the allocation request does not succeed).
如果该方法导致了重分配,那么分配将使用容器自带的分配器,这有可能在分配失败的时候导致异常(例如默认的分配器,在请求分配失败的时候会抛出bad_alloc异常)。
Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
// vector::reserve
#include <iostream>
#include <vector>
int main ()
{
std::vector<int>::size_type sz;
std::vector<int> foo;
sz = foo.capacity();
std::cout << "making foo grow:\n";
for (int i=0; i<100; ++i) {
foo.push_back(i);
if (sz!=foo.capacity()) {
sz = foo.capacity();
std::cout << "capacity changed: " << sz << '\n';
}
}
std::vector<int> bar;
sz = bar.capacity();
bar.reserve(100); // this is the only difference with foo above
std::cout << "making bar grow:\n";
for (int i=0; i<100; ++i) {
bar.push_back(i);
if (sz!=bar.capacity()) {
sz = bar.capacity();
std::cout << "capacity changed: " << sz << '\n';
}
}
return 0;
}
|
|
Possible output:
making foo grow:
capacity changed: 1
capacity changed: 2
capacity changed: 4
capacity changed: 8
capacity changed: 16
capacity changed: 32
capacity changed: 64
capacity changed: 128
making bar grow:
capacity changed: 100
|
Complexity
If a reallocation happens, linear invector sizeat most.
复杂度
如果重分配发生,时间或者空间复杂度为线性复杂度。
Iterator validity
If a reallocation happens, all iterators, pointers and references related to the container are invalidated.
一旦发生重分配,那么之前所获得的所有的迭代器以及引用都将失效!
Otherwise, they all keep referring to the same elements they were referring to before the call.
如果不发生重分配,那么都有效。
Data races
If a reallocation happens, the container and all its contained elements are modified.
Otherwise, the container is accessed, but not the contained elements: concurrently accessing or modifying them is safe.
一旦发生重分配,那么容器以及其内的所有元素都将被修改(存储位置)。
否则,容器将被访问,但其内元素不会被访问,修改以及访问他们都是安全的。
Exception safety
If no reallocations happen or if the type of the elements has either a non-throwing move constructor or a copy constructor, there are no changes in the container in case of exception (strong guarantee).
如果不发生重分配或者元素类型的移动构造函数以及复制构造器都不会抛出异常,,那么容器发生异常的规则不变。
Otherwise, the container is guaranteed to end in a valid state (basic guarantee).
The function throwslength_errorifnis greater thanmax_size.
此外,容器必须保证其结尾是一个有效的状态(这句翻译的好像不太好),当n大于max_size的时候,该方法会抛出一个length_error异常。
//翻译的不好的地方请多多指导,可以在下面留言或者点击左上方邮件地址给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。
转载请注明出处:http://blog.csdn.net/qq844352155
2014-8-12
于GDUT
分享到:
相关推荐
STL vector 知识详解 STL vector 知识详解 STL vector 知识详解 STL vector 知识详解 STL vector 知识详解
SGI STL之vector源码,带注释
c++的STL的vector的一个实现。使用了c++11的大部分特性,包含vector的几乎所有功能。仅作学习之用。
该文档详细讲解了C++中标准容器的使用,是一份不错的学习资料哦
STL中vector、list、deque和map的区别
使用VC++控制台应用程序编写,测试了:vector对象的排序,对象中的大小无序,有重复。
c++ STL source code, hash and vector etc
心希盼 c++ STL Vector 类源码 详细说明“心希盼 Vector.doc”
C++STL vector list map set dqueue 等应用举例及PPT讲解示例,代码演示
vector list map pair stl 标准模板库 c++ 程序示例
SGI vector源码
给大家介绍 stl vector用法,主要知识点在如何恰当的使用它们的成员函数,涉及到条件函数和函数指针在迭代算法中的使用,对stl vector用法感兴趣的朋友可以参考下本
详细讲解了STL中vector容器的用法.
STL VECTOR 、迭代器,DEV C++ 编译运行
非常实用的STL容器讲解学习,内容全,讲解详细 包括Vector、Vector、String、Deque、sort、set、map,绝对有用!!
实现统计一段文章的每个单词的个数 其中CountDemo使用STL中的Map来实现的 CountDemo2是用一般语言实现,没有用到STL实现的; MapCount是用STL中的Vector和Map共同实现的...此题目是学习STL中的Map和Vector必练的经典题目
仿写C++ STL标准库 vector 源码,可直接在cpp文件中调用实现
标准模板库中map、vector、以及sort等的用法讲解
STL 中的常用的Vector Map Set Sort用法
C++ 利用MAP和VECTOR实现多节点树,VC++ 利用STL中的MAP和VECTOR实现的一个多节点树。