emplace_back与push_back的区别
2014-08-14 21:31
281 查看
std::vector::emplace_back
C++Containers library
std::vector
template< class... Args > void emplace_back( Args&&... args ); | (since C++11) | |
If the new size() is greater than capacity() then all iterators and references (including the past-the-end iterator) are invalidated. Otherwise only the past-the-end iterator is invalidated.
Parameters
args | - | arguments to forward to the constructor of the element |
Return value
(none)Complexity
Constant.Exceptions
If an exception is thrown, this function has no effect (strong exception guarantee). IfT's move constructor is not noexcept and is not
CopyInsertableinto
*this, vector will use the throwing move constructor. If it throws, the guarantee is waived and the effects are unspecified.
Notes
The specialization std::vector<bool> did not haveemplace_back()member until C++14.
Example
The following code usesemplace_backto append an object of type
Presidentto a std::vector. It demonstrates how
emplace_backforwards parameters to the
Presidentconstructor and shows how using
emplace_backavoids the extra copy or move operation required when using
push_back.
#include <vector> #include <string> #include <iostream> struct President { std::string name; std::string country; int year; President(std::string p_name, std::string p_country, int p_year) : name(std::move(p_name)), country(std::move(p_country)), year(p_year) { std::cout << "I am being constructed.\n"; } President(President&& other) : name(std::move(other.name)), country(std::move(other.country)), year(other.year) { std::cout << "I am being moved.\n"; } President& operator=(const President& other) = default; }; int main() { std::vector<President> elections; std::cout << "emplace_back:\n"; elections.emplace_back("Nelson Mandela", "South Africa", 1994); std::vector<President> reElections; std::cout << "\npush_back:\n"; reElections.push_back(President("Franklin Delano Roosevelt", "the USA", 1936)); std::cout << "\nContents:\n"; for (President const& president: elections) { std::cout << president.name << " was elected president of " << president.country << " in " << president.year << ".\n"; } for (President const& president: reElections) { std::cout << president.name << " was re-elected president of " << president.country << " in " << president.year << ".\n"; } }
Output:
emplace_back: I am being constructed. push_back: I am being constructed. I am being moved. Contents: Nelson Mandela was elected president of South Africa in 1994. Franklin Delano Roosevelt was re-elected president of the USA in 1936.
相关文章推荐
- emplace_back() 和 push_back 的区别
- C++ - "emplace_back" 和 "push_back" 的区别
- C++ 中"emplace_back" 与 "push_back" 的区别
- 【C/C++开发】emplace_back() 和 push_back 的区别
- emplace_back和push_back的区别[转]
- C++ - "emplace_back" 和 "push_back" 的区别
- C++11中emplace_back和push_back的区别
- push_back和emplace_back的区别
- emplace_back() 和 push_back 的区别
- 论Java的ArrayList.add(e)和C++的vector.push_back(e)的区别
- 实战c++中的vector系列--知道emplace_back为何优于push_back吗?
- C++容器之emplace_back和push_back
- C++11使用emplace_back代替push_back
- C++11使用emplace_back代替push_back
- C++ emplace_back 和 push_back 的区别
- 实战c++中的vector系列--知道emplace_back为何优于push_back吗?
- Life is short, use emplace_back()。探究一下C++11的push_back, move, rvalue, emplace_back
- 论Java的ArrayList.add(e)和C++的vector.push_back(e)的区别
- c++ 11中emplace_back替代push_back的相关知识点,含右值引用,move用法等
- C++11使用emplace_back代替push_back