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
noneIf 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.
容器将被修改。
如果发生重分配,所有容器内元素都将被修改。
否则,不会访问容器内元素,同时访问以及修改他们都是安全的。
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(???)。将会导致为定义的行为。
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
- STL vector中的push_back方法(17)
- vector中的push_back()学习
- vector中的push_back
- vector::push_back 方法分析
- GNU STL vector push_back insert
- accumulate, copy, vector::push_back(STL Sample)
- STL vector中的rbegin方法(5)
- STL vector中的cbegin()方法(6)
- STL vector中的crbegin方法(7)
- STL vector中的rend方法(10)
- STL vector中的crend()方法(11)
- STL vector中的reserve()方法(15)
- STL vector中的clear方法(18)
- STL vector中的at方法(20)
- STL vector中的pop_back方法(22)
- STL vector中的emplace_back方法(24)
- STL vector中的erase方法(26)
- STL vector中的swap方法(33)
- 正则表达式笔记 2 边界符中的单词边界 \b
- Ueditor一加载就报Uncaught TypeMismatchError: Failed to execute 'removeAttributeNode' on 'Element'
- mongodb——java封装(id自增,gridFS)
- quick-cocos2d-x游戏开发【5】——创建菜单
- 学习pthreads,使用条件变量进行多线程之间的同步
- STL vector中的push_back方法(17)
- 正则表达式笔记 3 贪婪、勉强、独占三种匹配量词
- 【UML】UML视频总结——实战前的思考
- POJ 3468 A Simple Problem with Integers
- oracle RAC data guard 理论基础
- 基于大数据分析的网络攻击检测
- cxf快速发布webService之aegis
- va_start(), va_arg(), va_end() 宏定义的解释
- Jquery的模拟事件,input模拟focus