std::basic_string_view<CharT,Traits>::basic_string_view
constexpr basic_string_view() noexcept; |
(1) | (since C++17) |
constexpr basic_string_view( const basic_string_view& other ) noexcept = default; |
(2) | (since C++17) |
constexpr basic_string_view( const CharT* s, size_type count ); |
(3) | (since C++17) |
constexpr basic_string_view( const CharT* s ); |
(4) | (since C++17) |
template< class It, class End > constexpr basic_string_view( It first, End last ); |
(5) | (since C++20) |
template< class R > constexpr basic_string_view( R&& r ); |
(6) | (since C++23) |
constexpr basic_string_view( std::nullptr_t ) = delete; |
(7) | (since C++23) |
1) Default constructor. Constructs an empty
basic_string_view
. After construction,
data()
is equal to
nullptr
, and
size()
is equal to
0
.
2) Copy constructor. Constructs a view of the same content as
other
. After construction,
data()
is equal to
other.data()
, and
size()
is equal to
other.size()
.
3) Constructs a view of the first
count
characters of the character array starting with the element pointed by
s
.
s
can contain null characters. The behavior is undefined if
[s, s+count)
is not a valid range (even though the constructor may not access any of the elements of this range). After construction,
data()
is equal to
s
, and
size()
is equal to
count
.
4) Constructs a view of the null-terminated character string pointed to by
s
, not including the terminating null character. The length of the view is determined as if by
Traits::length(s)
. The behavior is undefined if
[s, s+Traits::length(s))
is not a valid range. After construction,
data()
is equal to
s
, and
size()
is equal to
Traits::length(s)
.
5) Constructs a
basic_string_view
over the range
[first, last)
. The behavior is undefined if
[first, last)
is not a valid range, if
It
does not actually model
contiguous_iterator
, or if
End
does not actually model
sized_sentinel_for
for
It
. After construction,
data()
is equal to
std::to_address(first)
, and
size()
is equal to
(last - first)
.
This overload participates in overload resolution only if.
-
It
satisfiescontiguous_iterator
, -
End
satisfiessized_sentinel_for
forIt
, -
std::iter_value_t<It>
andCharT
are the same type, and -
End
is not convertible tostd::size_t
.
6) Constructs a
basic_string_view
over the range
r
. After construction,
data()
is equal to
ranges::data(r)
, and
size()
is equal to
ranges::size(r)
.
This overload participates in overload resolution only if.
-
std::remove_cvref_t<R>
is not the same type asbasic_string_view
, -
R
modelscontiguous_range
andsized_range
, -
ranges::range_value_t<R>
andCharT
are the same type, -
R
is not convertible toconst CharT*
, - let
d
be an lvalue of typestd::remove_cvref_t<R>
,d.operator ::std::basic_string_view<CharT, Traits>()
is not a valid expression, and - if qualified-id
std::remove_reference_t<R>::traits_type
is valid and denotes a type, it is same asTraits
.
7)
basic_string_view
cannot be constructed from
nullptr
.
Parameters
other | - | another view to initialize the view with |
s | - | pointer to a character array or a C string to initialize the view with |
count | - | number of characters to include in the view |
first | - | iterator to the first character of the sequence |
last | - | iterator past the last character of the sequence or another sentinel |
r | - | a contiguous range that contains the sequence |
Complexity
1-3,5-6) constant
4) linear in length of
s
Example
#include <array> #include <iomanip> #include <iostream> #include <string> #include <string_view> int main() { constexpr std::wstring_view wcstr_v = L"xyzzy"; // overload (4) std::cout << "wcstr_v.size(): " << wcstr_v.size() << '\n'; char array[3] = {'B', 'a', 'r'}; std::string_view array_v(array, std::size(array)); // overload (3) std::cout << "array_v: " << quoted( array_v ) << '\n'; std::string cppstr = "Foo"; std::string_view cppstr_v(cppstr); // (2), after std::string::operator string_view std::cout << "cppstr_v: " << quoted( cppstr_v ) << '\n'; std::array ar = {'P', 'u', 'b'}; std::string_view ar_v(ar.begin(), ar.end()); // (5), C++20 std::cout << "ar_v: " << quoted( ar_v ) << '\n'; // std::string_view ar_v2{ar}; // (6), OK in C++23 // std::cout << "ar_v2: " << quoted( ar_v2 ) << '\n'; // ar_v2: "Pub" const char* one_0_two = "One\0Two"; std::string_view one_v {one_0_two}; // (4) std::cout << "one_v: " << quoted( one_v ) << ", one_v.size(): " << one_v.size() << '\n'; std::string_view one_two_v {one_0_two, 7}; // (3) std::cout << "one_two_v: \""; for (char c: one_two_v) { std::cout << (c != '\0' ? c : '?'); } std::cout << "\", one_two_v.size(): " << one_two_v.size() << '\n'; }
Output:
wcstr_v.size(): 5 array_v: "Bar" cppstr_v: "Foo" ar_v: "Pub" one_v: "One", one_v.size(): 3 one_two_v: "One?Two", one_two_v.size(): 7
See also
(C++17)
|
assigns a view (public member function) |
constructs a basic_string (public member function of std::basic_string<CharT,Traits,Allocator> ) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/string/basic_string_view/basic_string_view