|
|
Defined in header <string_view> | ||
template<class CharT,class Traits> std::basic_ostream<CharT, Traits>& | (since C++17) | |
Behaves as aFormattedOutputFunction. After constructing and checking the sentry object,determines the output format padding.
Then stores each character from the resulting sequenceseq (the contents ofv with 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.
Contents |
May throwstd::ios_base::failure if an exception is thrown during output.
os | - | a character output stream |
v | - | the view to be inserted |
os
#include <iomanip>#include <iostream>#include <string_view> int main(){constexprstd::string_view s{"abc"};constexprint width{5}; // fill/left/right properties are kept until changedstd::cout<<std::setfill('-');std::cout<<std::left; std::cout<<'['<<std::setw(width)<< s<<"]\n";std::cout<<'['<<std::setw(width)<< s<<"]\n"; std::cout<<std::right;std::cout<<'['<<std::setw(width)<< s<<"]\n"; // width is reset after each callstd::cout<<'['<< s<<"]\n";}
Output:
[abc--][abc--][--abc][abc]
performs stream input and output on strings (function template)[edit] |