std::vector::push_back
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.
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).
参数
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
|
|
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