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::operator>> - cpprefjp C++日本語リファレンス

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

最終更新日時: 2026年02月20日 15時50分20秒 (JST)
Akira Takahashi が更新

履歴 編集

function template
<string>

std::operator>>

namespace std {
  template <class CharT, class Traits, class Allocator>
  std::basic_istream<CharT, Traits>&
    operator>>(std::basic_istream<CharT, Traits>& is,
               basic_string<CharT, Traits, Allocator>& str);
}

概要

文字列をストリームから入力する。空白文字が現れるまで、あるいはsetwマニピュレータで指定された数までの文字列を入力する。

この関数は、書式化入力関数(basic_istreamを参照)として作用する。

効果

  1. basic_istream<>::sentryオブジェクトを構築する。sentryオブジェクトが失敗を示した場合、何もしない。
  2. str.erase()を呼び出す。
  3. 以下のいずれかを満たすまで、文字を入力してstr.append(1, c)と等価な方法で文字列に追加する。なお、cは入力した文字を表す。
    • 最大文字数まで書き込んだ。最大文字数は次のいずれかである。
      • is.width()が0以上ならその値とする。
      • さもなくば、str.max_size()とする。
    • EOFに達した。
    • 空白文字に達した。空白文字の判定にはストリームに設定されているロケールが考慮される。
  4. is.width(0)を呼び出す。

1文字も入力が行われなかった場合、ローカルエラー状態にios_base::failbitを設定する。

戻り値

  • 仮引数is。

例

#include <iostream>
#include <string>

int main() {
  std::string a, b;
  if (std::cin >> a >> b) {
    std::cout << a << std::endl;
    std::cout << b << std::endl;
  }
}

入力

Apple Banana

出力

Apple
Banana

参照

  • getline: このほかのbasic_stringに対する入力関数。
  • このほかの>>演算子関数
    • <istream>ヘッダで定義されているもの
  • P1264R2 Revising the wording of stream input operations
    • C++23でローカルエラー状態の概念が導入され、入力関数のエラー処理セマンティクスが明確化された
You are visitor 01402520 — 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.