Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::getline

      From cppreference.com
      <cpp‎ |string‎ |basic string
       
       
       
      std::basic_string
       
      Defined in header<string>
      template<class CharT,class Traits,class Allocator>

      std::basic_istream<CharT, Traits>&
          getline(std::basic_istream<CharT, Traits>& input,

                   std::basic_string<CharT, Traits, Allocator>& str, CharT delim);
      (1)
      template<class CharT,class Traits,class Allocator>

      std::basic_istream<CharT, Traits>&
          getline(std::basic_istream<CharT, Traits>&& input,

                   std::basic_string<CharT, Traits, Allocator>& str, CharT delim);
      (2)(since C++11)
      template<class CharT,class Traits,class Allocator>

      std::basic_istream<CharT, Traits>&
          getline(std::basic_istream<CharT, Traits>& input,

                   std::basic_string<CharT, Traits, Allocator>& str);
      (3)
      template<class CharT,class Traits,class Allocator>

      std::basic_istream<CharT, Traits>&
          getline(std::basic_istream<CharT, Traits>&& input,

                   std::basic_string<CharT, Traits, Allocator>& str);
      (4)(since C++11)

      getline reads characters from an input stream and places them into a string:

      1,2) Behaves asUnformattedInputFunction, except thatinput.gcount() is not affected. After constructing and checking the sentry object, performs the following:
      1) Callsstr.erase().
      2) Extracts characters frominput and appends them tostr until one of the following occurs (checked in the order listed)
      a) end-of-file condition oninput, in which case,getline setseofbit.
      b) the next available input character isdelim, as tested byTraits::eq(c, delim), in which case the delimiter character is extracted frominput, but is not appended tostr.
      c)str.max_size() characters have been stored, in which casegetline setsfailbit and returns.
      3) If no characters were extracted for whatever reason (not even the discarded delimiter),getline setsfailbit and returns.
      3,4) Same asgetline(input, str, input.widen('\n')), that is, the default delimiter is the endline character.

      Contents

      [edit]Parameters

      input - the stream to get data from
      str - the string to put the data into
      delim - the delimiter character

      [edit]Return value

      input

      [edit]Notes

      When consuming whitespace-delimited input (e.g.int n;std::cin>> n;) any whitespace that follows, including a newline character, will be left on the input stream. Then when switching to line-oriented input, the first line retrieved withgetline will be just that whitespace. In the likely case that this is unwanted behaviour, possible solutions include:

      [edit]Example

      The following example demonstrates how to use thegetline function to read user input, and to process a stream line by line, or by parts of a line using thedelim parameter.

      Run this code
      #include <iostream>#include <sstream>#include <string> int main(){// greet the userstd::string name;std::cout<<"What is your name? ";    std::getline(std::cin, name);std::cout<<"Hello "<< name<<", nice to meet you.\n"; // read file line by linestd::istringstream input;    input.str("1\n2\n3\n4\n5\n6\n7\n");int sum=0;for(std::string line; std::getline(input, line);)        sum+=std::stoi(line);std::cout<<"\nThe sum is "<< sum<<".\n\n"; // use separator to read parts of the linestd::istringstream input2;    input2.str("a;b;c;d");for(std::string line; std::getline(input2, line,';');)std::cout<< line<<'\n';}

      Possible output:

      What is your name? John Q. PublicHello John Q. Public, nice to meet you. The sum is 28. abcd

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      LWG 91C++98getline did not behave as an unformatted input functionbehaves as an unformatted input function

      [edit]See also

      extracts characters until the given character is found
      (public member function ofstd::basic_istream<CharT,Traits>)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/string/basic_string/getline&oldid=171694"

      [8]ページ先頭

      ©2009-2025 Movatter.jp