|
|
|
Defined in header <string> | ||
template<class CharT,class Traits,class Allocator> std::basic_ostream<CharT, Traits>& | (1) | |
template<class CharT,class Traits,class Allocator> std::basic_istream<CharT, Traits>& | (2) | |
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) |
N
characters are read, whereN
isis.width() ifis.width()>0, otherwiseN
isstr.max_size(),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 |
os | - | a character output stream |
is | - | a character input stream |
str | - | the string to be inserted or extracted |
#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!
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
LWG 25 | C++98 | n was the smaller ofos.width() andstr.size() | n is the larger of them |
LWG 90 | C++98 | std::isspace(c, getloc()) was used to check for spaces, but getloc is not declared in<string> | replacedgetloc() withis.getloc() |
LWG 91 | C++98 | operator>> did not behaveas aFormattedInputFunction | behaves as a FormattedInputFunction |
LWG 211 | C++98 | operator>> did not setfailbit if no character is extracted | setsfailbit |
LWG 435 | C++98 | characters 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 586 | C++98 | operator<< did not behaveas aFormattedOutputFunction | behaves as a FormattedOutputFunction |
(C++17) | performs stream output on string views (function template)[edit] |