Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      operator>>(std::basic_istream)

      From cppreference.com
      <cpp‎ |io‎ |basic istream
       
       
       
       
      Defined in header<istream>
      template<class CharT,class Traits>

      basic_istream<CharT, Traits>&
          operator>>( basic_istream<CharT, Traits>& st, CharT& ch);

      template<class Traits>
      basic_istream<char, Traits>&
          operator>>( basic_istream<char, Traits>& st,signedchar& ch);

      template<class Traits>
      basic_istream<char, Traits>&

          operator>>( basic_istream<char, Traits>& st,unsignedchar& ch);
      (1)
      (2)
      template<class CharT,class Traits>

      basic_istream<CharT, Traits>&
          operator>>( basic_istream<CharT, Traits>& st, CharT* s);

      template<class Traits>
      basic_istream<char, Traits>&
          operator>>( basic_istream<char, Traits>& st,signedchar* s);

      template<class Traits>
      basic_istream<char, Traits>&

          operator>>( basic_istream<char, Traits>& st,unsignedchar* s);
      (until C++20)
      template<class CharT,class Traits,std::size_t N>

      basic_istream<CharT, Traits>&
          operator>>( basic_istream<CharT, Traits>& st, CharT(&s)[N]);

      template<class Traits,std::size_t N>
      basic_istream<char, Traits>&
          operator>>( basic_istream<char, Traits>& st,signedchar(&s)[N]);

      template<class Traits,std::size_t N>
      basic_istream<char, Traits>&

          operator>>( basic_istream<char, Traits>& st,unsignedchar(&s)[N]);
      (since C++20)
      template<class Istream,class T>

      Istream&&

          operator>>( Istream&& st, T&& value);
      (3)(since C++11)
      1,2) Performs character input operations.
      1) Behaves as aFormattedInputFunction. After constructing and checking the sentry object, which may skip leading whitespace, extracts a character and stores it toch. If no character is available, setsfailbit (in addition toeofbit that is set as required of aFormattedInputFunction).
      2) Behaves as aFormattedInputFunction. After constructing and checking the sentry object, which may skip leading whitespace, extracts successive characters and stores them at successive locations ofa character array whose first element is pointed to by(until C++20)s. The extraction stops if any of the following conditions is met:
      • A whitespace character (as determined by thectype<CharT> facet) is found. The whitespace character is not extracted.
      • Ifst.width() is greater than zero,st.width()-1 characters are stored.
      (until C++20)
      • n-1 characters are stored, wheren is defined as follows:
      (since C++20)
      • End of file occurs in the input sequence (this also setseofbit).
      In either case, an additional null character valueCharT() is stored at the end of the output. If no characters were extracted, setsfailbit (the null character is still written, to the first position in the output). Finally, callsst.width(0) to cancel the effects ofstd::setw, if any.
      3) Calls the appropriate extraction operator, given an rvalue reference to an input stream object (equivalent tost>>std::forward<T>(value)). This overload participates in overload resolution only ifst>>std::forward<T>(value) is well-formed andIstream is a class type publicly and unambiguously derived fromstd::ios_base.

      Contents

      [edit]Notes

      Extracting a single character that is the last character of the stream does not seteofbit: this is different from other formatted input functions, such as extracting the last integer withoperator>>, but this behavior matches the behavior ofstd::scanf with"%c" format specifier.

      [edit]Parameters

      st - input stream to extract the data from
      ch - reference to a character to store the extracted character to
      s -pointer to(until C++20) a character array to store the extracted characters to

      [edit]Return value

      1,2)st
      3)std::move(st)

      [edit]Example

      Run this code
      #include <iomanip>#include <iostream>#include <sstream> int main(){std::string input="n greetings";std::istringstream stream(input); char c;constint MAX=6;char cstr[MAX];     stream>> c>>std::setw(MAX)>> cstr;std::cout<<"c = "<< c<<'\n'<<"cstr = "<< cstr<<'\n'; double f;std::istringstream("1.23")>> f;// rvalue stream extractionstd::cout<<"f = "<< f<<'\n';}

      Output:

      c = ncstr = greetf = 1.23

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      LWG 13C++98the definition ofn mentioned a non-existing nameeosreplaced withCharT()
      LWG 68C++98no null characters were stored at the end of the output for overload (2)stores a null character
      LWG 1203C++98overload for rvalue stream returned lvalue reference to the base classreturns rvalue reference
      to the derived class
      LWG 2328C++98overload for rvalue stream required another argument to be lvaluemade to accept rvalue
      LWG 2534C++98overload for rvalue stream was not constrainedconstrained

      [edit]See also

      extracts formatted data
      (public member function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/io/basic_istream/operator_gtgt2&oldid=158522"

      [8]ページ先頭

      ©2009-2025 Movatter.jp