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>::vector
std::vector<T,Allocator>::vector
| (1) | ||
| vector(); |
(C++17 å) | |
| vector() noexcept(noexcept(Allocator())); |
(C++17 èµ·) | |
| (2) | ||
| explicit vector( const Allocator& alloc ); |
(C++17 å) | |
| explicit vector( const Allocator& alloc ) noexcept; |
(C++17 èµ·) | |
| (3) | ||
| explicit vector( size_type count, Â Â Â Â Â Â Â Â Â const T& value = T(), |
(C++11 å) | |
| Â Â Â Â Â Â Â Â Â vector( size_type count, Â Â Â Â Â Â Â Â Â const T& value, |
(C++11 èµ·) | |
| (4) | ||
| explicit vector( size_type count ); |
(C++11 èµ·) (C++14 å) |
|
| explicit vector( size_type count, const Allocator& alloc = Allocator() ); |
(C++14 èµ·) | |
| template< class InputIt > vector( InputIt first, InputIt last, |
(5) | |
| vector( const vector& other ); |
(6) | |
| vector( const vector& other, const Allocator& alloc ); |
(6) | (C++11 èµ·) |
| (7) | ||
| vector( vector&& other ); |
(C++11 èµ·) (C++17 å) |
|
| vector( vector&& other ) noexcept; |
(C++17 èµ·) | |
| vector( vector&& other, const Allocator& alloc ); |
(8) | (C++11 èµ·) |
| vector( std::initializer_list<T> init, Â Â Â Â const Allocator& alloc = Allocator() ); |
(9) | (C++11 èµ·) |
ä»åç§æ°æ®æºæé æ°å®¹å¨ï¼å¯éå°ä½¿ç¨ç¨æ·æä¾çåé
å¨ alloc ã
alloc ç空容å¨ãcount 个æå¼ value çå
ç´ ç容å¨ã[first, last) å
容ç容å¨ã
|
è¥ |
(C++11 å) |
|
æ¤éè½½ä»
è¥ |
(C++11 èµ·) |
other å
容ç容å¨ãè¥ä¸æä¾ alloc ï¼åå¦åéè¿è°ç¨ std::allocator_traits<allocator_type>::select_on_container_copy_construction(other.get_allocator()) è·å¾åé
å¨ãother å
容ç容å¨ãåé
å¨éè¿å±äº other çåé
å¨ç§»å¨æé è·å¾ãç§»å¨åï¼ä¿è¯ other 为 empty() ãalloc 为æ°å®¹å¨çåé
å¨ï¼ä» other ç§»å¨å
容ï¼è¥ alloc != other.get_allocator() ï¼åå®å¯¼è´éå
ç´ ç§»å¨ãï¼è¯¥æ
åµä¸ï¼ç§»å¨åä¸ä¿è¯ other 为空ï¼init å
容ç容å¨ãåæ°
| alloc | - | ç¨äºæ¤å®¹å¨ææå ååé çåé å¨ |
| count | - | 容å¨çå¤§å° |
| value | - | 以ä¹åå§å容å¨å ç´ çå¼ |
| first, last | - | å¤å¶å ç´ çæ¥æºèå´ |
| other | - | ç¨ä½åå§å容å¨å ç´ æ¥æºçå¦ä¸å®¹å¨ |
| init | - | ç¨ä½åå§åå ç´ æ¥æºç initializer_list |
å¤æåº¦
count æçº¿æ§first å last çè·ç¦»æçº¿æ§other çå¤§å°æçº¿æ§init çå¤§å°æçº¿æ§ãå¼å¸¸
å° Allocator::allocate çè°ç¨å¯è½æåºã
注æ
å¨å®¹å¨ç§»å¨æé ï¼éè½½ (7) ï¼åï¼æå other çå¼ç¨åè¿ä»£å¨ï¼é¤äºå°¾è¿ä»£å¨ï¼ä¿æåæ³ï¼ä½æä»£ç°äº *this ä¸çå
ç´ ãå½åæ åç± [container.requirements.general]/12 ä¸çæ»æ¬éè¿°ä½åºæ¤ä¿è¯ï¼è LWG 2321 æ£å¨èèæ´ä¸¥æ ¼çä¿è¯ã
éè½½ (4) å¯¹å¦ int çé类类åå
ç´ æ¸
é¶ï¼è¿ä¸ new[] å°å
ç´ ä¿ææªåå§åçè¡ä¸ºä¸åã为å¹é
new[] çè¡ä¸ºï¼å¯æä¾ä¿çå
ç´ æªåå§åçèªå®ä¹ Allocator::construct ã
示ä¾
#include <vector> #include <string> #include <iostream>  template<typename T> std::ostream& operator<<(std::ostream& s, const std::vector<T>& v) { s.put('['); char comma[3] = {'\0', ' ', '\0'}; for (const auto& e : v) { s << comma << e; comma[0] = ','; } return s << ']'; }  int main() { // c++11 åå§åå¨åè¡¨è¯æ³ï¼ std::vector<std::string> words1 {"the", "frogurt", "is", "also", "cursed"}; std::cout << "words1: " << words1 << '\n';  // words2 == words1 std::vector<std::string> words2(words1.begin(), words1.end()); std::cout << "words2: " << words2 << '\n';  // words3 == words1 std::vector<std::string> words3(words1); std::cout << "words3: " << words3 << '\n';  // words4 为 {"Mo", "Mo", "Mo", "Mo", "Mo"} std::vector<std::string> words4(5, "Mo"); std::cout << "words4: " << words4 << '\n'; }
è¾åºï¼
words1: [the, frogurt, is, also, cursed] words2: [the, frogurt, is, also, cursed] words3: [the, frogurt, is, also, cursed] words4: [Mo, Mo, Mo, Mo, Mo]
ç¼ºé·æ¥å
ä¸åæ´æ¹è¡ä¸ºçç¼ºé·æ¥å追溯å°åºç¨äºä»¥ååºçç C++ æ åã
| DR | åºç¨äº | åºçæ¶çè¡ä¸º | æ£ç¡®è¡ä¸º |
|---|---|---|---|
| LWG 2193 | C++11 | é»è®¤æé 彿°ä¸º explicit | 使ä¹ä¸ºé explicit |
åé
| å°å¼èµç»å®¹å¨ (å ¬å¼æå彿°) | |
| èµå¼ç»å®¹å¨ (å ¬å¼æå彿°) |