std::string_view constructors
std::basic_string_view class can be constructed in many different ways.
Use the button in the top-right corner to navigate with arrows for convenience.
std::basic_string_view is a class template, with following type parameters,
that are used within constructors:
| pub | CharT | Character type |
| pub | Traits |
important Like for |
Default constructor
- C++20
- Simplified
- Detailed
basic_string_view();
- this constructor is
constexprandnoexcept
constexpr basic_string_view() noexcept;
Default constructor. Constructs an empty string view with:
data() == nullptrsize() == 0
Complexity
Constant - O(1).
Example
#include <string_view>
#include <iostream>
#include <iomanip>
int main() {
// Default constructed string view
std::string_view sv;
std::cout << sv.size() << ' ' << std::quoted(sv);
}
0 ""
Defaulted copy constructor
- C++20
constexpr basic_string_view( const basic_string_view& other ) noexcept
= default;
Copy constructor. Constructs a view of the same content as other, which has:
data() == other.data()size() == other.size()
Complexity
Constant - O(1).
Example
#include <string_view>
#include <iostream>
#include <iomanip>
int main() {
std::string_view sv = "Hello";
std::string_view sv2 = sv;
std::cout << sv.size() << ' ' << std::quoted(sv) << '\n';
std::cout << sv2.size() << ' ' << std::quoted(sv2);
}
5 "Hello"
5 "Hello"
Char pointer constructor (bound)
- C++20
constexpr basic_string_view( const CharT* s, size_type count );
Constructs a view of the first count characters of the character array starting with the element pointed by s. The created view has:
data() == ssize() == count
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)
s can contain null characters.
Complexity
Constant - O(1).
Example
#include <string_view>
#include <iostream>
#include <iomanip>
int main() {
std::string_view sv("Hello World!", 5);
std::cout << sv.size() << ' ' << std::quoted(sv) << '\n';
}
5 "Hello"
Char pointer constructor (unbound)
- C++20
constexpr basic_string_view( const CharT* s );
data() == ssize() == Traits::length(s)
The behavior is undefined if:
- [ s, s + Traits::length(s) ) is not a valid range (even though the constructor may not access any of the elements of this range)
The length of the view is determined as if by Traits::length(s).
Complexity
Constant - O(1).
Example
#include <string_view>
#include <iostream>
#include <iomanip>
int main() {
const char* string = "Hello\0 World!";
std::string_view sv(string);
std::cout << sv.size() << ' ' << std::quoted(sv) << '\n';
}
5 "Hello"
Copy range constructor (iterators)
- C++20
template< class It, class End >
constexpr basic_string_view( It first, End last );
Constructs a string view over the range [ first, last ). The resulting view has:
data() == std::to_address(first)size() == last - first
The behavior is undefined if:
- **[ first, last ) is not a valid range
Itdoes not modelcontiguous_iterator
orEnddoes not modelsized_sentinel_for
Overload resolution
Complexity
Constant - O(1).
Example
#include <string_view>
#include <iostream>
int main() {
std::string s = "Hello World!";
std::string_view sv(s.begin(), s.begin()+5);
std::string_view sv2(s.begin() + 6, s.end());
std::cout << sv << ' ' << sv2;
}
Hello World!
Copy range constructor (range)
- C++23
template< class R >
explicit constexpr basic_string_view( R&& r );
Constructs a string view over the range r.
The resulting view has:
data() == ranges::data(r)size() == ranges::size(r)
Overload resolution
Complexity
Constant - O(1).
Example
#include <string_view>
#include <iostream>
int main() {
std::string s = "Hello";
std::string_view sv(s);
std::cout << sv;
}
Hello
Deleted std::nullptr_t constructor
- C++23
constexpr basic_string_view( std::nullptr_t ) = delete;
A deleted constructor that prohibits constructing a string view from a nullptr.
Example
#include <string_view>
int main() {
// std::string_view sv(nullptr); // Won't compile!
}
Hover to see the original license.