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

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

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

履歴 編集

function template
<string>

std::getline

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

  template <class CharT, class Traits, class Allocator>
  std::basic_istream<CharT, Traits>&
    getline(std::basic_istream<CharT, Traits>&& is,
            basic_string<CharT, Traits, Allocator>& str);              // (2) C++11から

  template <class CharT, class Traits, class Allocator>
  std::basic_istream<CharT, Traits>&
    getline(std::basic_istream<CharT, Traits>& is,
            basic_string<CharT, Traits, Allocator>& str, CharT delim); // (3)

  template <class CharT, class Traits, class Allocator>
  std::basic_istream<CharT, Traits>&
    getline(std::basic_istream<CharT, Traits>&& is,
            basic_string<CharT, Traits, Allocator>& str, CharT delim); // (4) C++11から
}

概要

ストリームから改行文字が現れるまで(1行すべて)あるいは仮引数delimで指定された文字までの文字列を入力する。

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

効果

仮引数delimがないオーバーロードでは、std::getline(is, str, is.widen('\n'))を呼び出す。

仮引数delimを持つものは以下の通り。

  1. basic_istream<>::sentryオブジェクトを構築する。sentryオブジェクトが失敗を示した場合、何もしない。
  2. str.erase()を呼び出す。
  3. 以下のいずれかを満たすまで、文字を入力してstr.append(1, c)と等価な方法で文字列に追加する。なお、cは入力した文字を表す。以下の条件判断はこの順で行う。
    1. EOFに達した。この場合、ローカルエラー状態にios_base::eofbitを設定する。
    2. Traits::eq(c, delim)が真となった。
    3. 読み取った文字数がstr.max_size()になるまで書き込んだ。この場合、ローカルエラー状態にios_base::failbitを設定する。
  4. 1文字も入力がなされなかったら、ローカルエラー状態にios_base::failbitを設定する。
    • 空行の場合はこれに該当しないことに注意。なぜなら、改行文字1文字を入力しているためである。

この関数はbasic_istream<>::gcountには影響を及ぼさない。

戻り値

  • 仮引数is。

例

#include <iostream>
#include <string>

int main() {
  std::string s;
  if (std::getline(std::cin, s)) {
    std::cout << s << std::endl;
  }
}

入力

Haru wa akebono.

出力

Haru wa akebono.

関連項目

名前 説明
std::basic_istream<>::getline 文字の配列へ入力を行うもの

参照

  • P1264R2 Revising the wording of stream input operations
    • C++23でローカルエラー状態の概念が導入され、入力関数のエラー処理セマンティクスが明確化された
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.