Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::basic_istream<CharT,Traits>::operator>>

      From cppreference.com
      <cpp‎ |io‎ |basic istream
       
       
       
       
      basic_istream& operator>>(unsignedshort& value);
      (1)
      basic_istream& operator>>(unsignedint& value);
      (2)
      basic_istream& operator>>(long& value);
      (3)
      basic_istream& operator>>(unsignedlong& value);
      (4)
      basic_istream& operator>>(longlong& value);
      (5)(since C++11)
      basic_istream& operator>>(unsignedlonglong& value);
      (6)(since C++11)
      basic_istream& operator>>(float& value);
      (7)
      basic_istream& operator>>(double& value);
      (8)
      basic_istream& operator>>(longdouble& value);
      (9)
      basic_istream& operator>>(bool& value);
      (10)
      basic_istream& operator>>(void*& value);
      (11)
      basic_istream& operator>>(short& value);
      (12)
      basic_istream& operator>>(int& value);
      (13)
      basic_istream& operator>>(/* extended-floating-point-type */& value);
      (14)(since C++23)
      basic_istream& operator>>(std::ios_base&(*func)(std::ios_base&));
      (15)
      basic_istream& operator>>(std::basic_ios<CharT, Traits>&
                                     (*func)(std::basic_ios<CharT, Traits>&));
      (16)
      basic_istream& operator>>( basic_istream&(*func)(basic_istream&));
      (17)
      basic_istream& operator>>(std::basic_streambuf<CharT, Traits>* sb);
      (18)

      Extracts values from an input stream.

      1-11) Extracts a value potentially skipping preceding whitespace. The value is stored to a given referencevalue.
      This function behaves as aFormattedInputFunction. After constructing and checking the sentry object, which may skip leading whitespace, extracts a value by callingstd::num_get::get().
      12) Extracts ashort value potentially skipping preceding whitespace. The value is stored to a given referencevalue.
      This function behaves as aFormattedInputFunction. After constructing and checking the sentry object, which may skip leading whitespace, extracts along valuelval by callingstd::num_get::get(). After that:
      13) Extracts anint value potentially skipping preceding whitespace. The value is stored to a given referencevalue.
      This function behaves as aFormattedInputFunction. After constructing and checking the sentry object, which may skip leading whitespace, extracts along valuelval by callingstd::num_get::get(). After that:
      14) Extracts an extended floating-point value potentially skipping preceding whitespace. The value is stored to a given referencevalue. The library provides overloads for all cv-unqualifiedextended floating-point types as the referenced type of the parametervalue.
      Determines the standard floating-point typeFP as follows:
      • If thefloating-point conversion rank of/* extended-floating-point-type */ is less than or equal to that offloat, thenFP isfloat.
      • Otherwise, if the floating-point conversion rank of/* extended-floating-point-type */ is less than or equal to that ofdouble, thenFP isdouble.
      • Otherwise,FP islongdouble.
      This function behaves as aFormattedInputFunction. After constructing and checking the sentry object, which may skip leading whitespace, extracts anFP valuefval by callingstd::num_get::get(). After that:
      • Iffval<-std::numeric_limits</* extended-floating-point-type */>::max(), setsfailbit and stores-std::numeric_limits</* extended-floating-point-type */>::max() toval.
      • Otherwise, ifstd::numeric_limits</* extended-floating-point-type */>::max()< fval, setsfailbit and storesstd::numeric_limits</* extended-floating-point-type */>::max() toval.
      • Otherwise, storesstatic_cast</* extended-floating-point-type */>(fval) toval.
      15-17) Callsfunc(*this), wherefunc is an I/O manipulator.
      18) Behaves as anUnformattedInputFunction. After constructing and checking the sentry object, extracts all data from*this and stores it tosb. The extraction stops if one of the following conditions are met:
      • end-of-file occurs on the input sequence;
      • inserting in the output sequence fails (in which case the character to be inserted is not extracted);
      • an exception occurs (in which case the exception is caught, and only rethrown if it inserted no characters andfailbit is enabled inexceptions()).
      In either case, stores the number of characters extracted in the member variable accessed by subsequent calls togcount(). Ifsb is a null pointer or if no characters were inserted intosb, callssetstate(failbit) (which may throwstd::ios_base::failure if enabled).

      If extraction fails (e.g. if a letter was entered where a digit is expected), zero is written tovalue andfailbit is set. For signed integers, if extraction results in the value too large or too small to fit invalue,std::numeric_limits<T>::max() orstd::numeric_limits<T>::min() (respectively) is written andfailbit flag is set. For unsigned integers, if extraction results in the value too large or too small to fit invalue,std::numeric_limits<T>::max() is written andfailbit flag is set.

      Contents

      [edit]Parameters

      value - reference to an integer or floating-point value to store the extracted value to
      func - pointer to I/O manipulator function
      sb - pointer to the stream buffer to write all the data to

      [edit]Return value

      1-16,18)*this
      17)func(*this)

      [edit]Notes

      For overload(14), when the extended floating-point type has a floating-point conversion rank that is not equal to the rank of any standard floating-point type, then double rounding during the conversion can result in inaccurate results.std::from_chars() can be used in situations where maximum accuracy is important.

      [edit]Example

      Run this code
      #include <iomanip>#include <iostream>#include <sstream> int main(){std::string input="41 3.14 false hello world";std::istringstream stream(input); int n;double f;bool b;     stream>> n>> f>>std::boolalpha>> b;std::cout<<"n = "<< n<<'\n'<<"f = "<< f<<'\n'<<"b = "<<std::boolalpha<< b<<'\n'; // extract the rest using the streambuf overload    stream>>std::cout.rdbuf();std::cout<<'\n';}

      Output:

      n = 41f = 3.14b = falsehello world

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      LWG 64C++98it was unclear whether overload(18) can only rethrow the
      std::ios_base::failure thrown by callingsetstate(failbit)
      all exceptions caught
      can be rethrown
      LWG 118C++98overload(12,13) delegated the extraction tonum_get::get,
      but it does not have overloads forshort andint
      along value is extracted
      instead ofshort orint
      LWG 413C++98overload(18) only rethrew exceptions thrown while extracting
      characters fromsb, but characters are extracted from*this
      correctedsb to*this
      LWG 567C++98overload(18) behaved as aFormattedInputFunction
      because of the resolution ofLWG issue 60
      it behaves as an
      UnformattedInputFunction
      LWG 661C++98overloads(12,13) did not store the extracted number
      tovalue due to the resolution ofLWG issue 118
      stores the number if
      no overflow occurs
      LWG 696C++98value was unchanged on extraction failureset to zero or minimum/
      maximum values

      [edit]See also

      extracts characters and character arrays
      (function template)[edit]
      performs stream input and output on strings
      (function template)[edit]
      performs stream input and output of bitsets
      (function template)[edit]
      serializes and deserializes a complex number
      (function template)[edit]
      performs stream input and output on pseudo-random number engine
      (function template)[edit]
      performs stream input and output on pseudo-random number distribution
      (function template)[edit]
      extracts blocks of characters
      (public member function)[edit]
      extracts already available blocks of characters
      (public member function)[edit]
      extracts characters
      (public member function)[edit]
      extracts characters until the given character is found
      (public member function)[edit]
      (C++17)
      converts a character sequence to an integer or floating-point value
      (function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/io/basic_istream/operator_gtgt&oldid=148931"

      [8]ページ先頭

      ©2009-2025 Movatter.jp