STL vector中的push_back方法(17)
2014-08-14 11:13
956 查看
原文地址:http://www.cplusplus.com/reference/vector/vector/push_back/
public member function
<vector>
C++98
C++11
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的时候才会发生重分配。
valValue 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的模版参数指定。
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异常)
The example uses push_back to add a new element to the vector each time a new integer is read.这个例子里面,每当读取一个整数输入时,都使用push_back来将其加入到vector中。
Constant (amortized time, reallocation may happen).
If a reallocation happens, the reallocation is itself up to linear in the entire size.
如果发生重分配,重分配过程是线性是时间复杂度。
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.
否则,只有超尾迭代器失效,之前所获得的其他迭代器仍然有效。
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 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
public member function
<vector>
std::vector::push_back
C++98C++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
valValue 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
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; } | Edit & Run |
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
相关文章推荐
- push_back使用方法
- vector::push_back 方法分析
- STL vector中的pop_back方法(22)
- C++/STL_中的push_back方法与复制数据的问题
- C++ push方法与push_back方法 浅析
- STL vector中的emplace_back方法(24)
- C++ push方法与push_back方法 浅析
- Js:数组的各种方法范例pop,push,unshift,splice,shift
- Java网络编程从入门到精通(17):Socket类的getter和setter方法(1)
- 使用javascript重定向后让back按钮有效的方法
- Grub erro 17的解决方法
- 数组自带的push方法
- WebForm_PostBackOptions未定义 解决方法
- WebForm_PostBackOptions未定义 的原因及解决方法
- 开发工具异常之server2008中页面报错webform_postbackoptions未定义解决方法(原创)
- 在MIDP2.0中使用Push注册机制,允许应用被自动启动的方法
- Js:数组的各种方法范例pop,push,unshift,splice,shift
- 关于RDA的Push和Pull方法多种情况的测试结果
- 【转】“给你第二次机会”——小议PushbackInputStream
- js 的push 方法