From cppreference.com
template<classCharT,classTraits>std::basic_ostream<CharT,Traits>&operator<<(std::basic_ostream<CharT,Traits>&os,std::basic_string_view<CharT,Traits>v);(since C++17)Behaves as a
. After constructing and checking the sentry object,
determines the output format padding
.
Then stores each character from the resulting sequence seq (the contents of v with padding) to the output stream os as if by calling os.rdbuf()->sputn(seq,n), where n is std::max(os.width(),str.size()).
Finally, calls os.width(0) to cancel the effects of
, if any.
Exceptions
May throw
if an exception is thrown during output.
Parameters
os - a character output stream v - the view to be inserted Return value
os
Example
Run this code
#include<iomanip>#include<iostream>#include<string_view>intmain(){constexprstd::string_views{"abc"};constexprintwidth{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] See also