Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      operator<<,>>(std::basic_string)

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

      std::basic_ostream<CharT, Traits>&
          operator<<(std::basic_ostream<CharT, Traits>& os,

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

      std::basic_istream<CharT, Traits>&
          operator>>(std::basic_istream<CharT, Traits>& is,

                     std::basic_string<CharT, Traits, Allocator>& str);
      (2)
      1) Behaves as aFormattedOutputFunction. After constructing and checking the sentry object,determines the output format padding.

      Then inserts each character from the resulting sequenceseq (the contents ofstr plus padding) to the output streamos as if by callingos.rdbuf()->sputn(seq, n), wheren isstd::max(os.width(), str.size())Finally, callsos.width(0) to cancel the effects ofstd::setw, if any.

      Equivalent toreturn os<<std::basic_string_view<CharT, Traits>(str);.

      (since C++17)
      2) Behaves as aFormattedInputFunction. After constructing and checking the sentry object, which may skip leading whitespace, first clearsstr withstr.erase(), then reads characters fromis and appends them tostr as if bystr.append(1, c), until one of the following conditions becomes true:
      • N characters are read, whereN isis.width() ifis.width()>0, otherwiseN isstr.max_size(),
      • the end-of-file condition occurs in the streamis, or
      • std::isspace(c, is.getloc()) istrue for the next characterc inis (this whitespace character remains in the input stream).

      If no characters are extracted thenstd::ios::failbit is set onis, which may throwstd::ios_base::failure.

      Finally, callsis.width(0) to cancel the effects ofstd::setw, if any.

      Contents

      [edit]Exceptions

      1) May throwstd::ios_base::failure if an exception is thrown during output.
      2) May throwstd::ios_base::failure if no characters are extracted fromis (e.g. the stream is at end of file, or consists of whitespace only), or if an exception is thrown during input.

      [edit]Parameters

      os - a character output stream
      is - a character input stream
      str - the string to be inserted or extracted

      [edit]Return value

      1)os
      2)is

      [edit]Example

      Run this code
      #include <iostream>#include <sstream>#include <string> int main(){std::string greeting="Hello, whirled!";std::istringstream iss(greeting); std::string hello_comma, whirled, word;     iss>> hello_comma;    iss>> whirled; std::cout<< greeting<<'\n'<< hello_comma<<'\n'<< whirled<<'\n'; // Reset the stream    iss.clear();    iss.seekg(0); while(iss>> word)std::cout<<'+'<< word<<'\n';}

      Output:

      Hello, whirled!Hello,whirled!+Hello,+whirled!

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      LWG 25C++98n was the smaller ofos.width() andstr.size()n is the larger of them
      LWG 90C++98std::isspace(c, getloc()) was used to check
      for spaces, butgetloc is not declared in<string>
      replacedgetloc()
      withis.getloc()
      LWG 91C++98operator>> did not behave
      as aFormattedInputFunction
      behaves as a
      FormattedInputFunction
      LWG 211C++98operator>> did not setfailbit if no character is extractedsetsfailbit
      LWG 435C++98characters were inserted byos.rdbuf()->sputn(str.data(), n),
      and the resolution ofLWG issue 25 made the behavior
      undefined ifos.width() is larger thanstr.size()
      determines the padding
      first and inserts the padded
      character sequence instead
      LWG 586C++98operator<< did not behave
      as aFormattedOutputFunction
      behaves as a
      FormattedOutputFunction

      [edit]See also

      (C++17)
      performs stream output on string views
      (function template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/string/basic_string/operator_ltltgtgt&oldid=152690"

      [8]ページ先頭

      ©2009-2025 Movatter.jp