STL vector中的push_back方法(17)


public member function
<vector>

std::vector::push_back

void push_back (const value_type& val);
void push_back (value_type&& val);
Add element at the end
Adds a new element at the end of the  vector, after its current last element. The content of  val is copied (or moved) to the new element.
该函数将一个新的元素加到vector的最后面,位置为当前最后一个元素的下一个元素,新的元素的值是val的拷贝(或者是移动拷贝).

This effectively increases the container  size by one, which causes an automatic reallocation of the allocated storage space if -and only if- the new vector  size surpasses the current vector  capacity.
该方法可以快速有效率地在数组size范围内增长元素,除非当增长的元素个数大小超出了vector的ccapacity的时候才会发生重分配。

Parameters

val
Value to be copied (or moved) to the new element.
Member type  value_type is the type of the elements in the container, defined in  vector as an alias of its first template parameter ( T).
参数
新元素的值。
类型由vector的模版参数指定。

Return value

none

If a reallocation happens, the storage is allocated using the container's allocator, which may throw exceptions on failure (for the default allocatorbad_alloc is thrown if the allocation request does not succeed).
如果发生了重分配,将使用容器的分配器进行内存分配,这可能会抛出异常。(例如allocator这个默认的分配器在请求失败时会抛出bad_alloc异常)

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// vector::push_back
#include <iostream>
#include <vector>

int main ()
{
  std::vector<int> myvector;
  int myint;

  std::cout << "Please enter some integers (enter 0 to end):\n";

  do {
    std::cin >> myint;
    myvector.push_back (myint);
  } while (myint);

  std::cout << "myvector stores " << int(myvector.size()) << " numbers.\n";

  return 0;
}

The example uses push_back to add a new element to the vector each time a new integer is read.这个例子里面,每当读取一个整数输入时,都使用push_back来将其加入到vector中。

Complexity

Constant (amortized time, reallocation may happen).

If a reallocation happens, the reallocation is itself up to linear in the entire size.
如果发生重分配,重分配过程是线性是时间复杂度。

Iterator validity

If a reallocation happens, all iterators, pointers and references related to the container are invalidated.如果发生重分配,之前所获得的所有迭代器,指针以及引用都将失效。
Otherwise, only the end iterator is invalidated, and all iterators, pointers and references to elements are guaranteed to keep referring to the same elements they were referring to before the call.
否则,只有超尾迭代器失效,之前所获得的其他迭代器仍然有效。

Data races

The container is modified.
If a reallocation happens, all contained elements are modified.
Otherwise, no existing element is accessed, and concurrently accessing or modifying them is safe.
容器将被修改。
如果发生重分配,所有容器内元素都将被修改。
否则,不会访问容器内元素,同时访问以及修改他们都是安全的。

Exception safety

If no reallocations happen, there are no changes in the container in case of exception (strong guarantee).如果没有发生重分配,容器抛出异常的规则不变。
If a reallocation happens, the strong guarantee is also given if the type of the elements is either copyable or no-throw moveable.如果发生重分配,如果元素类型的复制构造器以及移动构造器不会抛出异常,那么规则也不变
Otherwise, the container is guaranteed to end in a valid state (basic guarantee).
If allocator_traits::construct is not supported with val as argument, it causes undefined behavior.
否则,容器只保证在一个有效的状态下结束。(???)如果allocator_traits::construct不支持val(???)。将会导致为定义的行为。

//翻译的不好的地方请多多指导,可以在下面留言或者点击左上方邮件地址给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。


转载请注明出处:http://blog.csdn.net/qq844352155

2014-8-14

于GDUT





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值