operator<<(std::basic_string_view)
From cppreference.com
< cpp | string | basic string view
C++
Feature test macros (C++20)
Concepts library (C++20)
Metaprogramming library (C++11)
Ranges library (C++20)
Filesystem library (C++17)
Concurrency support library (C++11)
Execution control library (C++26)
std::basic_string_view
(C++20)
(C++20)
(C++23)
Constants
(until C++20)(until C++20)(until C++20)(until C++20)(until C++20)(C++20)
operator<<
Deduction guides (C++20)
Defined in header
<string_view>
template< class CharT, class Traits >
(since C++17)
std::basic_ostream <CharT, Traits>&
operator<<( std::basic_ostream <CharT, Traits>& os,
Behaves as a FormattedOutputFunction. 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 std::setw , if any.
[edit] Exceptions
May throw std::ios_base::failure if an exception is thrown during output.
[edit] Parameters
os
-
a character output stream
v
-
the view to be inserted
[edit] Return value
os
[edit] Example
Run this code
#include <iomanip> #include <iostream> #include <string_view> int main() { constexpr std::string_view s{"abc"}; constexpr int width{5}; // fill/left/right properties are kept until changed std::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 call std::cout << '[' << s << "]\n"; }
Output:
[abc--] [abc--] [--abc] [abc]