STL vector中的push_back方法(17)

来源:互联网 发布:淘宝有哪些美食 编辑:程序博客网 时间:2024/05/22 05:12
原文地址:http://www.cplusplus.com/reference/vector/vector/push_back/

public member function
<vector>

std::vector::push_back

  • C++98
  • C++11
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 allocator, bad_alloc is thrown if the allocation request does not succeed).
如果发生了重分配,将使用容器的分配器进行内存分配,这可能会抛出异常。(例如allocator这个默认的分配器在请求失败时会抛出bad_alloc异常)

Example

1234567891011121314151617181920
// 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;}
Edit & Run

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 0