std::string_view 构造函数
std::basic_string_view
类可以通过多种不同的方式构造。使用右上角的按钮,方便地使用箭头导航。
std::basic_string_view
是一个**类模板**,具有以下类型参数,这些参数在构造函数中使用
公共 | CharT | 字符类型 |
公共 | Traits |
重要 与 |
默认构造函数
- C++20
- 简化
- 详细
basic_string_view();
- 此构造函数是
constexpr
和noexcept
constexpr basic_string_view() noexcept;
默认构造函数。构造一个空的字符串视图,具有
data() == nullptr
size() == 0
复杂度
常数 - O(1)。
示例
#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 ""
默认复制构造函数
- C++20
constexpr basic_string_view( const basic_string_view& other ) noexcept
= default;
复制构造函数。构造一个与 other
内容相同的视图,其具有
data() == other.data()
size() == other.size()
复杂度
常数 - O(1)。
示例
#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"
字符指针构造函数 (有界)
- C++20
constexpr basic_string_view( const CharT* s, size_type count );
构造一个视图,包含字符数组中从 s
指向的元素开始的前 count
个字符。创建的视图具有
data() == s
size() == count
行为未定义
如果- [ s, s + count ) 不是有效范围(即使构造函数可能不访问此范围中的任何元素)
s
可以包含空字符。
复杂度
常数 - O(1)。
示例
#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"
字符指针构造函数 (无界)
- C++20
constexpr basic_string_view( const CharT* s );
data() == s
size() == Traits::length(s)
行为未定义
如果- [ s, s + Traits::length(s) ) 不是有效范围(即使构造函数可能不访问此范围中的任何元素)
视图的长度由 Traits::length(s)
确定。
复杂度
常数 - O(1)。
示例
#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"
复制范围构造函数 (迭代器)
- C++20
template< class It, class End >
constexpr basic_string_view( It first, End last );
构造一个覆盖范围 [ first, last ) 的字符串视图。结果视图具有
data() == std::to_address(first)
size() == last - first
行为未定义
如果- **[ first, last ) 不是有效范围
It
不符合contiguous_iterator
模型
或End
不符合sized_sentinel_for
模型
重载决议
这些重载仅在以下情况下参与重载决议
It
满足contiguous_iterator
End
满足It
的sized_sentinel_for
std::iter_value_t<It>
和CharT
是相同类型
和End
不可转换为std::size_t
复杂度
常数 - O(1)。
示例
#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!
复制范围构造函数 (范围)
- C++23
template< class R >
explicit constexpr basic_string_view( R&& r );
构造一个覆盖范围 r
的字符串视图。结果视图具有
data() == ranges::data(r)
size() == ranges::size(r)
重载决议
这些重载仅在以下情况下参与重载决议
std::remove_cvref_t<R>
与std::basic_string_view
不是相同类型R
建模contiguous_range
和sized_range
ranges::range_value_t<R>
和CharT
是相同类型R
不可转换为const CharT*
- 令
d
为类型std::remove_cvref_t<R>
的左值,则d.operator ::std::basic_string_view<CharT, Traits>()
不是有效表达式
和 - 如果限定 ID
std::remove_reference_t<R>::traits_type
有效并表示一种类型,则它与Traits
相同
复杂度
常数 - O(1)。
示例
#include <string_view>
#include <iostream>
int main() {
std::string s = "Hello";
std::string_view sv(s);
std::cout << sv;
}
Hello
已删除的std::nullptr_t
构造函数
- C++23
constexpr basic_string_view( std::nullptr_t ) = delete;
一个已删除的构造函数,禁止从 nullptr
构造字符串视图。
示例
#include <string_view>
int main() {
// std::string_view sv(nullptr); // Won't compile!
}