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>::resize
std::vector<T,Allocator>::resize
| void resize( size_type count, T value = T() ); |
(C++11 å) | |
| void resize( size_type count ); |
(1) | (C++11 èµ·) |
| void resize( size_type count, const value_type& value ); |
(2) | (C++11 èµ·) |
é设容å¨å¤§å°ä»¥å®¹çº³ count 个å
ç´ ã
è¥å½å大å°å¤§äº count ï¼ååå°å®¹å¨ä¸ºå
¶é¦ count 个å
ç´ ã
|
è¥å½å大å°å°äº |
(C++11 å) |
|
è¥å½å大å°å°äº 1) ååéé¢å¤çé»è®¤æå
¥çå
ç´
2) ååéé¢å¤ç value ç坿¬
|
(C++11 èµ·) |
åæ°
| count | - | 容å¨çå¤§å° |
| value | - | ç¨ä»¥åå§åæ°å ç´ çå¼ |
| ç±»åè¦æ± | ||
- 为使ç¨éè½½ (1) ï¼ T å¿
须满足å¯ç§»å¨æå
¥ (MoveInsertable) å å¯é»è®¤æå
¥ (DefaultInsertable) çè¦æ±ã
| ||
- 为使ç¨éè½½ (2) ï¼ T å¿
须满足å¯å¤å¶æå
¥ (CopyInsertable) çè¦æ±ã
| ||
è¿åå¼
ï¼æ ï¼
å¤æåº¦
ä¸å½å大å°å count é´çå·®æçº¿æ§ãè¥å®¹éå°äº count åå¯è½æéåé
æè´çé¢å¤å¤æåº¦ã
å¼å¸¸
è¥æåºå¼å¸¸ï¼åæ¤å½æ°æ ææï¼å¼ºå¼å¸¸ä¿è¯ï¼ã
|
éè½½ (1) ä¸ï¼è¥ |
(C++11 èµ·) |
注æ
è¥ä¸æ³è¦éè½½ (1) ä¸çå¼åå§åï¼ä¾å¦å
ç´ æ¯é类类åä¸ä¸éè¦æ¸
é¶ï¼åå¯ä»¥æä¾å®å¶ç Allocator::construct é¿å
ã
å¨é设大å°å°è¾å°å¼æ¶ï¼ vector ç容éå³ä¸åå°ï¼å 为è¿ä¼éæ³åææçï¼èéåªéæ³åçä»·ç pop_back() è°ç¨åºåæéæ³åçè¿ä»£å¨ã
示ä¾
#include <iostream> #include <vector> int main() { std::vector<int> c = {1, 2, 3}; std::cout << "The vector holds: "; for(auto& el: c) std::cout << el << ' '; std::cout << '\n'; c.resize(5); std::cout << "After resize up to 5: "; for(auto& el: c) std::cout << el << ' '; std::cout << '\n'; c.resize(2); std::cout << "After resize down to 2: "; for(auto& el: c) std::cout << el << ' '; std::cout << '\n'; }
è¾åºï¼
The vector holds: 1 2 3 After resize up to 5: 1 2 3 0 0 After resize down to 2: 1 2
åé
| è¿å容纳çå
ç´ æ° (å ¬å¼æå彿°) | |
| æå
¥å
ç´ (å ¬å¼æå彿°) | |
| æ¦é¤å
ç´ (å ¬å¼æå彿°) |