Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::basic_string<CharT,Traits,Allocator>::resize

      From cppreference.com
      <cpp‎ |string‎ |basic string
       
       
       
      std::basic_string
       
      void resize( size_type count);
      (1)(constexpr since C++20)
      void resize( size_type count, CharT ch);
      (2)(constexpr since C++20)

      Resizes the string to containcount characters.

      If the current size is less thancount, additional characters are appended:

      1) Initializes appended characters toCharT() ('\0' ifCharT ischar).
      2) Initializes appended characters toch.

      If the current size is greater thancount, the string is reduced to its firstcount elements.

      Contents

      [edit]Parameters

      count - new size of the string
      ch - character to initialize the new characters with

      [edit]Exceptions

      std::length_error ifcount> max_size() istrue.Any exceptions thrown by correspondingAllocator.

      If an exception is thrown for any reason, this function has no effect (strong exception safety guarantee).

      [edit]Example

      Run this code
      #include <iomanip>#include <iostream>#include <stdexcept> int main(){constunsigned desired_length{8};std::string long_string("Where is the end?");std::string short_string("H"); std::cout<<"Basic functionality:\n"<<"Shorten:\n"<<"1. Before: "<<std::quoted(long_string)<<'\n';    long_string.resize(desired_length);std::cout<<"2. After:  "<<std::quoted(long_string)<<'\n'; std::cout<<"Lengthen with a given value 'a':\n"<<"3. Before: "<<std::quoted(short_string)<<'\n';    short_string.resize(desired_length,'a');std::cout<<"4. After:  "<<std::quoted(short_string)<<'\n'; std::cout<<"Lengthen with char() == "<<static_cast<int>(char())<<'\n'<<"5. Before: "<<std::quoted(short_string)<<'\n';    short_string.resize(desired_length+3);std::cout<<"6. After:\"";for(char c: short_string)std::cout<<(c==char()?'@': c);std::cout<<"\"\n\n"; std::cout<<"Errors:\n";std::string s; try{// size is OK, no length_error// (may throw bad_alloc)        s.resize(s.max_size()-1,'x');}catch(conststd::bad_alloc& ex){std::cout<<"1. Exception: "<< ex.what()<<'\n';} try{// size is OK, no length_error// (may throw bad_alloc)        s.resize(s.max_size(),'x');}catch(conststd::bad_alloc& ex){std::cout<<"2. Exception: "<< ex.what()<<'\n';} try{// size is BAD, throw length_error        s.resize(s.max_size()+1,'x');}catch(conststd::length_error& ex){std::cout<<"3. Length error: "<< ex.what()<<'\n';}}

      Possible output:

      Basic functionality:Shorten:1. Before: "Where is the end?"2. After:  "Where is"Lengthen with a given value 'a':3. Before: "H"4. After:  "Haaaaaaa"Lengthen with char() == 05. Before: "Haaaaaaa"6. After:  "Haaaaaaa@@@" Errors:1. Exception: std::bad_alloc2. Exception: std::bad_alloc3. Length error: basic_string::_M_replace_aux

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      LWG 847C++98there was no exception safety guaranteeadded strong exception safety guarantee
      LWG 2250C++98the behavior was undefined if
      count> max_size() istrue
      always throws an exception in this case

      [edit]See also

      returns the number of characters
      (public member function)[edit]
      reserves storage
      (public member function)[edit]
      reduces memory usage by freeing unused memory
      (public member function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/string/basic_string/resize&oldid=178347"

      [8]ページ先頭

      ©2009-2025 Movatter.jp