• Class / Function / Type

      std::
    • Header file

      <>
    • Other / All

    最終更新日時(UTC):
    が更新

    履歴 編集

    function
    <string>

    std::basic_string::resize

    void resize(size_type n, charT c);           // (1) C++03
    constexpr void resize(size_type n, charT c); // (1) C++20
    
    void resize(size_type n);                    // (2) C++03
    constexpr void resize(size_type n);          // (2) C++20
    

    概要

    文字列の長さを変更する。

    要件

    n <= max_size()

    効果

    • n <= size() のとき、元の文字列末尾のsize() - n要素を削除する。
    • n > size() のとき、n - size()個のcを元の文字列末尾にコピーして追加する

    resize(n) は、 resize(n, charT()) と等しい。

    戻り値

    なし

    例外

    n > max_size() の時、length_error 例外を投げる。

    #include <iostream>
    #include <string>
    
    int main()
    {
      std::string s = "hello";
    
      // sの長さを10に変更。大きくなった場所には'x'を埋める。
      s.resize(10, 'x');
    
      std::cout << s << std::endl;
    }
    

    出力

    helloxxxxx
    

    参照