GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • cpprefjp - C++日本語リファレンス
  • GitHub Project

This view

  • Original page
  • New address

GeezerVison is showing cpprefjp.github.io — std::basic_string::resize - cpprefjp C++日本語リファレンス

  1. リファレンス
  2. string
  3. basic_string
  4. resize

最終更新日時: 2026年10月03日 22時31分04秒 (JST)
Akira Takahashi が更新

履歴 編集

function
<string>

std::basic_string::resize

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

void resize(size_type n);                    // (2) C++98
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

参照

  • LWG Issue 2318 basic_string's wording has confusing relics from the copy-on-write era
  • P1148R0 Cleaning up Clause 20
  • P0980R1 Making std::string constexpr
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.