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>::insert
std::vector<T,Allocator>::insert
| (1) | ||
| iterator insert( iterator pos, const T& value ); |
(C++11 å) | |
| iterator insert( const_iterator pos, const T& value ); |
(C++11 èµ·) | |
| iterator insert( const_iterator pos, T&& value ); |
(2) | (C++11 èµ·) |
| (3) | ||
| void insert( iterator pos, size_type count, const T& value ); |
(C++11 å) | |
| iterator insert( const_iterator pos, size_type count, const T& value ); |
(C++11 èµ·) | |
| (4) | ||
| template< class InputIt > void insert( iterator pos, InputIt first, InputIt last); |
(C++11 å) | |
| template< class InputIt > iterator insert( const_iterator pos, InputIt first, InputIt last ); |
(C++11 èµ·) | |
| iterator insert( const_iterator pos, std::initializer_list<T> ilist ); |
(5) | (C++11 èµ·) |
æå ¥å ç´ å°å®¹å¨ä¸çæå®ä½ç½®ã
1-2) å¨
pos åæå
¥ value ã3) å¨
pos åæå
¥ value ç count ä¸ªå¯æ¬ã4) å¨
è¥
pos åæå
¥æ¥èªèå´ [first, last) çå
ç´ ã
è¥ InputIt ä¸ºæ´æ°ç±»åï¼åæ¤éè½½ä¸éè½½ (3) æ¥æç¸åææã |
(C++11 å) |
æ¤éè½½ä»
è¥ InputIt 足以为éçè¾å
¥è¿ä»£å¨ (LegacyInputIterator) æåä¸éè½½å³è®®ï¼ä»¥é¿å
ä¸éè½½ (3) ææ§ä¹ã |
(C++11 èµ·) |
first å last æ¯æå *this ä¸çè¿ä»£å¨ï¼åè¡ä¸ºæªå®ä¹ã5) å¨
pos åæå
¥æ¥èª initializer_list ilist çå
ç´ ãè¥æ° size() å¤§äºæ§ capacity() å导è´éåé ã è¥æ°ç size() å¤§äº capacity() ï¼åææè¿ä»£å¨åå¼ç¨é½è¢«éæ³åãå¦åï¼ä» å¨æå ¥ç¹åçè¿ä»£å¨åå¼ç¨ä¿æåæ³ãå°¾åè¿ä»£å¨äº¦è¢«éæ³åã
åæ°
| pos | - | å°å
容æå
¥å°å
¶åçè¿ä»£å¨ã pos å¯ä¸º end() è¿ä»£å¨
|
| value | - | è¦æå ¥çå ç´ å¼ |
| first, last | - | è¦æå ¥çå ç´ èå´ï¼ä¸è½æ¯æåè°ç¨ insert æç¨ç容å¨ä¸çè¿ä»£å¨ |
| ilist | - | è¦æå ¥ç弿¥æºç initializer_list |
| ç±»åè¦æ± | ||
- 为使ç¨éè½½ (1) ï¼ T å¿
须满足å¯å¤å¶èµå¼ (CopyAssignable) å å¯å¤å¶æå
¥ (CopyInsertable) çè¦æ±ã
| ||
- 为使ç¨éè½½ (2) ï¼ T å¿
须满足å¯ç§»å¨èµå¼ (MoveAssignable) å å¯ç§»å¨æå
¥ (MoveInsertable) çè¦æ±ã
| ||
- 为使ç¨éè½½ (3) ï¼ T å¿
须满足å¯å¤å¶èµå¼ (CopyAssignable) å å¯å¤å¶æå
¥ (CopyInsertable) çè¦æ±ã
| ||
- 为使ç¨éè½½ (4,5) ï¼ T å¿
须满足å¯å°±ä½æé (EmplaceConstructible) çè¦æ±ã
| ||
- 为使ç¨éè½½ (4) ï¼ T å¿
须满足å¯ç§»å¨èµå¼ (MoveAssignable) å å¯ç§»å¨æå
¥ (MoveInsertable) çè¦æ±ãä»
è¥ InputIt 满足éçè¾å
¥è¿ä»£å¨ (LegacyInputIterator) ä½ä¸æ»¡è¶³éçååè¿ä»£å¨ (LegacyForwardIterator) æè¦æ±ã(C++17 å)
| ||
- 为使ç¨éè½½ (4,5) ï¼ T å¿
须满足å¯äº¤æ¢ (Swappable) ã å¯ç§»å¨èµå¼ (MoveAssignable) ã å¯ç§»å¨æé (MoveConstructible) å å¯ç§»å¨æå
¥ (MoveInsertable) çè¦æ±ã(C++17 èµ·)
| ||
è¿åå¼
1-2) æå被æå
¥
value çè¿ä»£å¨ã3) æåé¦ä¸ªè¢«æå
¥å
ç´ çè¿ä»£å¨ï¼æè¥ count==0 å为
pos ã4) æåé¦ä¸ªè¢«æå
¥å
ç´ çè¿ä»£å¨ï¼æè¥ first==last å为
pos ã5) æåé¦ä¸ªè¢«æå
¥å
ç´ çè¿ä»£å¨ï¼æè¥
ilist 为空å为 pos ãå¤æåº¦
1-2) 常æ°ï¼å ä¸
pos ä¸å®¹å¨ç»å°¾çè·ç¦»æçº¿æ§ã3) ä¸ count æçº¿æ§ï¼å ä¸
pos ä¸å®¹å¨ç»å°¾çè·ç¦»æçº¿æ§ã5) ä¸ ilist.size() æçº¿æ§ï¼å ä¸
pos ä¸å®¹å¨ç»å°¾çè·ç¦»æçº¿æ§ãå¼å¸¸
è¥å¨å°¾ç«¯æå ¥å个å ç´ æ¶æåºå¼å¸¸ï¼ä¸ T 为å¯å¤å¶æå ¥ (CopyInsertable) æ std::is_nothrow_move_constructible<T>::value 为 true ï¼åæ ææï¼å¼ºå¼å¸¸ä¿è¯ï¼ã
示ä¾
è¿è¡æ¤ä»£ç
#include <iostream> #include <vector>  void print_vec(const std::vector<int>& vec) { for (auto x: vec) { std::cout << ' ' << x; } std::cout << '\n'; }  int main () { std::vector<int> vec(3,100); print_vec(vec);  auto it = vec.begin(); it = vec.insert(it, 200); print_vec(vec);  vec.insert(it,2,300); print_vec(vec);  // "it" ä¸ååæ³ï¼è·åæ°å¼ï¼ it = vec.begin();  std::vector<int> vec2(2,400); vec.insert(it+2, vec2.begin(), vec2.end()); print_vec(vec);  int arr[] = { 501,502,503 }; vec.insert(vec.begin(), arr, arr+3); print_vec(vec); }
è¾åºï¼
100 100 100 200 100 100 100 300 300 200 100 100 100 300 300 400 400 200 100 100 100 501 502 503 300 300 400 400 200 100 100 100
åé
| (C++11) |
å使é å
ç´ (å ¬å¼æå彿°) |
| å°å
ç´ æ·»å å°å®¹å¨æ«å°¾ (å ¬å¼æå彿°) |