`
阿尔萨斯
  • 浏览: 4186443 次
社区版块
存档分类
最新评论

STL vector中的reserve()方法(15)

 
阅读更多

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




分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics