std::vector::push_back
STD::向量::Push[医]背
void push_back( const T& value | (1) | |
---|---|---|
void push_back( T&& value | (2) | (since C++11) |
追加给定元素value
容器的末端。
1%29新元素初始化为value
...
2%29value
被移到新元素中。
如果新的size()
大于capacity()
然后,所有迭代器和引用%28,包括过去-结束迭代器%29无效。否则,只有过去结束迭代器无效。
参数
value | - | the value of the element to append |
---|
类型要求
T必须满足CopyInsertable的要求才能使用过载%281%29。
T必须满足MoveInsertable的要求才能使用过载%282%29。
返回值
%280%29
复杂性
摊销常数
例外
如果抛出异常%28,这可能是Allocator::allocate()
或元素复制/移动构造函数/赋值%29,此函数不具有%28强异常保证%29的效果。
If T's move constructor is not noexcept and T is not CopyInsertable into *this, vector will use the throwing move constructor. If it throws, the guarantee is waived and the effects are unspecified. | (since C++11) |
---|
注记
一些实现也会抛出std::length_error
何时push_back
导致重新分配的值超过max_size
,由于隐式调用reserve(size()+1)
...
例
二次
#include <vector>
#include <iostream>
#include <iomanip>
int main()
{
std::vector<std::string> numbers;
numbers.push_back("abc"
std::string s = "def";
numbers.push_back(std::move(s)
std::cout << "vector holds: ";
for (auto&& i : numbers) std::cout << std::quoted(i) << ' ';
std::cout << "\nMoved-from string holds " << std::quoted(s) << '\n';
}
二次
产出:
二次
vector holds: "abc" "def"
Moved-from string holds ""
二次
另见
emplace_back (C++11) | constructs an element in-place at the end (public member function) |
---|---|
pop_back | removes the last element (public member function) |
© cppreference.com
在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。