C++ åèæå
- C++11
- C++14
- C++17
- C++20
- C++ ç¼è¯å¨æ¯ææ åµè¡¨
- ç¬ç«ä¸å®¿ä¸»å®ç°
- C++ è¯è¨
- C++ å ³é®è¯
- é¢å¤çå¨
- C++ æ ååºå¤´æä»¶
- å ·åè¦æ±
- åè½ç¹æ§æµè¯ (C++20)
- å·¥å ·åº
- ç±»åæ¯æï¼åºæ¬ç±»åãRTTIãç±»åç¹æ§ï¼
- æ¦å¿µåº (C++20)
- é误å¤ç
- 卿å å管ç
- æ¥æåæ¶é´å·¥å ·
- å符串åº
- 容å¨åº
- std::array
- std::vector
- std::vector<T,Allocator>::push_back
- std::vector<T,Allocator>::assign
- std::vector<T,Allocator>::get_allocator
- std::vector<T,Allocator>::operator[]
- std::vector<T,Allocator>::front
- std::vector<T,Allocator>::at
- std::vector<T,Allocator>::pop_back
- std::vector<T,Allocator>::end, std::vector<T,Allocator>::cend
- std::vector<T,Allocator>::vector
- std::vector<T,Allocator>::~vector
- std::vector<T,Allocator>::operator=
- std::vector<T,Allocator>::back
- std::vector<T,Allocator>::data
- std::vector<T,Allocator>::begin, std::vector<T,Allocator>::cbegin
- std::vector<T,Allocator>::rbegin, std::vector<T,Allocator>::crbegin
- std::vector<T,Allocator>::rend, std::vector<T,Allocator>::crend
- std::vector<T,Allocator>::empty
- std::vector<T,Allocator>::size
- std::vector<T,Allocator>::max_size
- std::vector<T,Allocator>::reserve
- std::vector<T,Allocator>::capacity
- std::vector<T,Allocator>::shrink_to_fit
- std::vector<T,Allocator>::clear
- std::vector<T,Allocator>::insert
- std::vector<T,Allocator>::emplace
- std::vector<T,Allocator>::erase
- std::vector<T,Allocator>::emplace_back
- std::vector<T,Allocator>::resize
- std::vector<T,Allocator>::swap
- std::swap(std::vector)
- std::erase, std::erase_if (std::vector)
- operator==,!=,<,<=,>,>=,<=>(std::vector)
- std::vector çæ¨å¯¼æå¼
- std::map
- std::unordered_map
- std::priority_queue
- std::span
- std::forward_list
- std::deque
- std::list
- std::set
- std::multiset
- std::multimap
- std::unordered_set
- std::unordered_multiset
- std::unordered_multimap
- std::stack
- std::queue
- std::vector<bool>
- ç»ç¹ææ (C++17)
- 注é
- è¿ä»£å¨åº
- èå´åº (C++20)
- ç®æ³åº
- æ°å¼åº
- è¾å ¥/è¾åºåº
- æä»¶ç³»ç»åº
- æ¬å°ååº
- æ£å表达å¼åº
- ååæä½åº
- çº¿ç¨æ¯æåº
- å®éªæ§ C++ ç¹æ§
- æç¨çèµæº
- ç´¢å¼
- std 符å·ç´¢å¼
- åç¨æ¯æ (C++20)
- C++ å ³é®è¯
ä½ç½®ï¼é¦é¡µ > C++ åèæå >容å¨åº >std::vector > std::vector<T,Allocator>::emplace_back
std::vector<T,Allocator>::emplace_back
| template< class... Args > void emplace_back( Args&&... args ); |
(C++11 èµ·) (C++17 å) |
|
| template< class... Args > reference emplace_back( Args&&... args ); |
(C++17 èµ·) | |
æ·»å æ°å
ç´ å°å®¹å¨å°¾ãå
ç´ éè¿ std::allocator_traits::construct æé ï¼å®å
¸åå°ç¨å¸ç½® new äºå®¹å¨ææä¾çä½ç½®å使é å
ç´ ãåæ° args... 以 std::forward<Args>(args)... 转åå°æé 彿°ã
è¥æ°ç size() å¤§äº capacity() ï¼åææè¿ä»£å¨åå¼ç¨ï¼å å«å°¾åè¿ä»£å¨ï¼é½è¢«éæ³åãå¦åä» å°¾åè¿ä»£å¨è¢«éæ³åã
åæ°
| args | - | 转åå°å ç´ æé 彿°çåæ° |
| ç±»åè¦æ± | ||
-value_type å¿
须满足å¯ç§»å¨æå
¥ (MoveInsertable) å å¯å°±ä½æé (EmplaceConstructible) çè¦æ±ã
| ||
è¿åå¼
|
ï¼æ ï¼ |
(C++17 å) |
|
å°è¢«æå ¥å ç´ çå¼ç¨ã |
(C++17 èµ·) |
å¤æåº¦
åæå¸¸æ°ã
å¼å¸¸
è¥æåºå¼å¸¸ï¼åæ¤å½æ°æ ææï¼å¼ºå¼å¸¸ä¿è¯ï¼ã
è¥ T çç§»å¨æé 彿°é noexcept ä¸éå¯å¤å¶æå
¥ (CopyInsertable) å° *this ï¼å vector å°ä½¿ç¨æåºçç§»å¨æé 彿°ãè¥å®æåºï¼åä¿è¯è¢«èå¼ï¼ä¸æææªæå®ã
注æ
å 为å¯è½åçååé
ï¼ emplace_back 对 vector è¦æ±å
ç´ ç±»å为å¯ç§»å¨æå
¥ (MoveInsertable) ã
ç¹å std::vector<bool> å¨ C++14 åæ emplace_back() æåã
示ä¾
ä¸å代ç ç¨ emplace_back åé President ç±»åå¯¹è±¡å° std::vector ã宿¼ç¤º emplace_back å¦ä½è½¬ååæ°å° President çæé 彿°ï¼å¹¶å±ç¤ºå¦ä½ç¨ emplace_back é¿å
ç¨ 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"; } }
è¾åºï¼
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.
åé
| å°å
ç´ æ·»å å°å®¹å¨æ«å°¾ (å ¬å¼æå彿°) |