C++ åèæå
- C++11
- C++14
- C++17
- C++20
- C++ ç¼è¯å¨æ¯ææ åµè¡¨
- ç¬ç«ä¸å®¿ä¸»å®ç°
- C++ è¯è¨
- C++ å ³é®è¯
- é¢å¤çå¨
- C++ æ ååºå¤´æä»¶
- å ·åè¦æ±
- åè½ç¹æ§æµè¯ (C++20)
- å·¥å ·åº
- ç±»åæ¯æï¼åºæ¬ç±»åãRTTIãç±»åç¹æ§ï¼
- æ¦å¿µåº (C++20)
- é误å¤ç
- 卿å å管ç
- æ¥æåæ¶é´å·¥å ·
- å符串åº
- std::basic_string
- std::basic_string<CharT,Traits,Allocator>::npos
- std::basic_string<CharT,Traits,Allocator>::at
- std::hash (std::string, std::wstring, std::u16string, std::u32string, std::pmr::string, std::pmr::wstring, std::pmr::u16string, std::pmr::u32string)
- std::basic_string<CharT,Traits,Allocator>::assign
- std::basic_string<CharT,Traits,Allocator>::get_allocator
- std::basic_string<CharT,Traits,Allocator>::substr
- std::basic_string<CharT,Traits,Allocator>::basic_string
- std::basic_string<CharT,Traits,Allocator>::operator=
- std::basic_string<CharT,Traits,Allocator>::operator[]
- std::basic_string<CharT,Traits,Allocator>::front
- std::basic_string<CharT,Traits,Allocator>::back
- std::basic_string<CharT,Traits,Allocator>::data
- std::basic_string<CharT,Traits,Allocator>::c_str
- std::basic_string<CharT,Traits,Allocator>::operator basic_string_view
- std::basic_string<CharT,Traits,Allocator>::begin, std::basic_string<CharT,Traits,Allocator>::cbegin
- std::basic_string<CharT,Traits,Allocator>::end, std::basic_string<CharT,Traits,Allocator>::cend
- std::basic_string<CharT,Traits,Allocator>::rbegin, std::basic_string<CharT,Traits,Allocator>::crbegin
- std::basic_string<CharT,Traits,Allocator>::rend, std::basic_string<CharT,Traits,Allocator>::crend
- std::basic_string<CharT,Traits,Allocator>::empty
- std::basic_string<CharT,Traits,Allocator>::size, std::basic_string<CharT,Traits,Allocator>::length
- std::basic_string<CharT,Traits,Allocator>::max_size
- std::basic_string<CharT,Traits,Allocator>::reserve
- std::basic_string<CharT,Traits,Allocator>::capacity
- std::basic_string<CharT,Traits,Allocator>::shrink_to_fit
- std::basic_string<CharT,Traits,Allocator>::clear
- std::basic_string<CharT,Traits,Allocator>::insert
- std::basic_string<CharT,Traits,Allocator>::erase
- std::basic_string<CharT,Traits,Allocator>::push_back
- std::basic_string<CharT,Traits,Allocator>::pop_back
- std::basic_string<CharT,Traits,Allocator>::append
- std::basic_string<CharT,Traits,Allocator>::operator+=
- std::basic_string<CharT,Traits,Allocator>::compare
- std::basic_string<CharT,Traits,Allocator>::starts_with
- std::basic_string<CharT,Traits,Allocator>::ends_with
- std::basic_string<CharT,Traits,Allocator>::replace
- std::basic_string<CharT,Traits,Allocator>::copy
- std::basic_string<CharT,Traits,Allocator>::resize
- std::basic_string<CharT,Traits,Allocator>::swap
- std::basic_string<CharT,Traits,Allocator>::find
- std::basic_string<CharT,Traits,Allocator>::rfind
- std::basic_string<CharT,Traits,Allocator>::find_first_of
- std::basic_string<CharT,Traits,Allocator>::find_first_not_of
- std::basic_string<CharT,Traits,Allocator>::find_last_of
- std::basic_string<CharT,Traits,Allocator>::find_last_not_of
- std::operator+(std::basic_string)
- std::swap(std::basic_string)
- std::literals::string_literals::operator""s
- std::erase, std::erase_if (std::basic_string)
- operator<<,>>(std::basic_string)
- std::getline
- operator==,!=,<,<=,>,>=,<=>(std::basic_string)
- std::stoi, std::stol, std::stoll
- std::stoul, std::stoull
- std::stof, std::stod, std::stold
- std::to_string
- std::to_wstring
- std::basic_string çæ¨å¯¼æå¼
- std::basic_string_view
- ç©ºç»æ¢åèå符串
- ç©ºç»æ¢å¤åèå符串
- ç©ºç»æ¢å®½å符串
- std::char_traits
- 注é
- 容å¨åº
- è¿ä»£å¨åº
- èå´åº (C++20)
- ç®æ³åº
- æ°å¼åº
- è¾å ¥/è¾åºåº
- æä»¶ç³»ç»åº
- æ¬å°ååº
- æ£å表达å¼åº
- ååæä½åº
- çº¿ç¨æ¯æåº
- å®éªæ§ C++ ç¹æ§
- æç¨çèµæº
- ç´¢å¼
- std 符å·ç´¢å¼
- åç¨æ¯æ (C++20)
- C++ å ³é®è¯
ä½ç½®ï¼é¦é¡µ > C++ åèæå >åç¬¦ä¸²åº >std::basic_string > std::basic_string<CharT,Traits,Allocator>::resize
std::basic_string<CharT,Traits,Allocator>::resize
| (1) | ||
| void resize( size_type count ); |
(C++20 å) | |
| constexpr void resize( size_type count ); |
(C++20 èµ·) | |
| (2) | ||
| void resize( size_type count, CharT ch ); |
(C++20 å) | |
| constexpr void resize( size_type count, CharT ch ); |
(C++20 èµ·) | |
é设 string 大å°ä»¥å« count 个å符ã
è¥å½å大å°å°äº count ï¼ååéé¢å¤çå符ã
è¥å½å大å°å¤§äº count ï¼å缩å string å°ä¸ºå
¶é¦ count 个å
ç´ ã
第ä¸çæ¬åå§åæ°å符为 CharT() ï¼ç¬¬äºçæ¬åå§åæ°å符为 ch ã
åæ°
| count | - | string çæ°å¤§å° |
| ch | - | ç¨ä»¥åå§åæ°å符çå符 |
è¿åå¼
ï¼æ ï¼
å¼å¸¸
è¥ count > max_size() å为 std::length_error ãå¯¹åº Allocator ææçä»»ä½å¼å¸¸ã
è¥å ä»»ä½åå æåºå¼å¸¸ï¼åæ¤å½æ°æ ææï¼å¼ºå¼å¸¸ä¿è¯ï¼ã (C++11 èµ·)
示ä¾
è¿è¡æ¤ä»£ç
#include <iostream> #include <stdexcept>  int main() { std::cout << "Basic functionality:\n"; const unsigned desired_length(8); std::string long_string( "Where is the end?" ); std::string short_string( "Ha" );  // 缩ç std::cout << "Before: \"" << long_string << "\"\n"; long_string.resize( desired_length ); std::cout << "After: \"" << long_string << "\"\n";  // å é¿ std::cout << "Before: \"" << short_string << "\"\n"; short_string.resize( desired_length, 'a' ); std::cout << "After: \"" << short_string << "\"\n";  std::cout << "\nErrors:\n"; { std::string s;  try { // å¤§å° OK ï¼æ length_error // ï¼å¯è½æ bad_alloc ï¼ s.resize(s.max_size() - 1, 'x'); } catch (const std::bad_alloc&) { std::cout << "1. bad alloc\n"; }  try { // å¤§å° OK ï¼æ length_error // ï¼å¯è½æ bad_alloc ï¼ s.resize(s.max_size(), 'x'); } catch (const std::bad_alloc& exc) { std::cout << "2. bad alloc\n"; }  try { // 大å°éè¯¯ï¼æåº length_error s.resize(s.max_size() + 1, 'x'); } catch (const std::length_error&) { std::cout << "3. length error\n"; } } }
å¯è½çè¾åºï¼
Basic functionality: Before: "Where is the end?" After: "Where is" Before: "Ha" After: "Haaaaaaa" Â Errors: 1. bad alloc 2. bad alloc 3. length error
å¤æåº¦
ä¸ string å¤§å°æçº¿æ§ã
åé
| è¿ååç¬¦æ° (å ¬å¼æå彿°) |