Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::ws

      From cppreference.com
      <cpp‎ |io‎ |manip
       
       
       
      Input/output manipulators
      Floating-point formatting
      Integer formatting
      Boolean formatting
      Field width and fill control
      Other formatting
      Whitespace processing
      Output flushing
      Status flags manipulation
      Time and money I/O
      (C++11)
      (C++11)
      (C++11)
      (C++11)
      Quoted manipulator
      (C++14)
       
      Defined in header<istream>
      template<class CharT,class Traits>
      std::basic_istream<CharT, Traits>& ws(std::basic_istream<CharT, Traits>& is);

      Discards leading whitespace from an input stream.

      Behaves as anUnformattedInputFunction, except thatis.gcount() is not modified. After constructing and checking the sentry object, extracts characters from the stream and discards them until any one of the following conditions occurs:

      • end of file condition occurs in the input sequence (in which case the function callssetstate(eofbit) but does not setfailbit; this does not apply if theeofbit is already set onis prior to the call tows, in which case the construction of the sentry object would setfailbit).
      • the next available characterc in the input sequence is not whitespace as determined bystd::isspace(c, is.getloc()). The non-whitespace character is not extracted.

      This is an input-only I/O manipulator, it may be called with an expression such asin>> std::ws for anyin of typestd::basic_istream.

      Contents

      [edit]Parameters

      is - reference to input stream

      [edit]Return value

      is (reference to the stream after extraction of consecutive whitespace).

      [edit]Notes

      Ifeofbit is set on the stream prior to the call, the construction of the sentry object will setfailbit.

      [edit]Example

      Run this code
      #include <iomanip>#include <iostream>#include <istream>#include <sstream>#include <string> int main(){for(constchar* str:{"     #1 test","\t #2 test","#3 test"}){std::string line;std::getline(std::istringstream{str}, line);std::cout<<"getline returns:\t"<<std::quoted(line)<<'\n'; std::istringstream iss{str};std::getline(iss>> std::ws, line);std::cout<<"ws + getline returns:\t"<<std::quoted(line)<<'\n';}}

      Output:

      getline returns:"     #1 test"ws + getline returns:"#1 test"getline returns:" #2 test"ws + getline returns:"#2 test"getline returns:"#3 test"ws + getline returns:"#3 test"

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      LWG 415C++98callingstd::ws might not construct the sentry
      object (insonsistent with other input functions)
      required to construct
      the sentry object

      [edit]See also

      extracts and discards 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/io/manip/ws&oldid=159630"

      [8]ページ先頭

      ©2009-2025 Movatter.jp