跳到主要内容

std::string_view 构造函数

std::basic_string_view 类可以通过多种不同的方式构造。使用右上角的按钮,方便地使用箭头导航。

注意

std::basic_string_view 是一个**类模板**,具有以下类型参数,这些参数在构造函数中使用

公共CharT字符类型
公共Traits

CharTraits 类,指定字符类型上的操作。

重要

std::basic_string一样,Traits::char_type 必须与 CharT 命名相同的类型,否则程序是病态的

.

默认构造函数

basic_string_view();
  • 此构造函数是 constexprnoexcept

默认构造函数。构造一个空的字符串视图,具有

  • 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 ""

默认复制构造函数

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"

字符指针构造函数 (有界)

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"

字符指针构造函数 (无界)

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"

复制范围构造函数 (迭代器)

template< class It, class End >
constexpr basic_string_view( It first, End last );

构造一个覆盖范围 [ first, last ) 的字符串视图。结果视图具有

  • data() == std::to_address(first)
  • size() == last - first
未定义行为

行为未定义

如果

重载决议

这些重载仅在以下情况下参与重载决议

复杂度

常数 - 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!

复制范围构造函数 (范围)

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_rangesized_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 构造函数

constexpr basic_string_view( std::nullptr_t ) = delete;

一个已删除的构造函数,禁止从 nullptr 构造字符串视图。

示例

#include <string_view>

int main() {
// std::string_view sv(nullptr); // Won't compile!
}
本文源自此 CppReference 页面。它可能因改进或编辑偏好而有所改动。点击“编辑此页面”查看本文档的所有更改。
悬停查看原始许可证。

std::string_view 构造函数

std::basic_string_view 类可以通过多种不同的方式构造。使用右上角的按钮,方便地使用箭头导航。

注意

std::basic_string_view 是一个**类模板**,具有以下类型参数,这些参数在构造函数中使用

公共CharT字符类型
公共Traits

CharTraits 类,指定字符类型上的操作。

重要

std::basic_string一样,Traits::char_type 必须与 CharT 命名相同的类型,否则程序是病态的

.

默认构造函数

basic_string_view();
  • 此构造函数是 constexprnoexcept

默认构造函数。构造一个空的字符串视图,具有

  • 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 ""

默认复制构造函数

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"

字符指针构造函数 (有界)

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"

字符指针构造函数 (无界)

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"

复制范围构造函数 (迭代器)

template< class It, class End >
constexpr basic_string_view( It first, End last );

构造一个覆盖范围 [ first, last ) 的字符串视图。结果视图具有

  • data() == std::to_address(first)
  • size() == last - first
未定义行为

行为未定义

如果

重载决议

这些重载仅在以下情况下参与重载决议

复杂度

常数 - 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!

复制范围构造函数 (范围)

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_rangesized_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 构造函数

constexpr basic_string_view( std::nullptr_t ) = delete;

一个已删除的构造函数,禁止从 nullptr 构造字符串视图。

示例

#include <string_view>

int main() {
// std::string_view sv(nullptr); // Won't compile!
}
本文源自此 CppReference 页面。它可能因改进或编辑偏好而有所改动。点击“编辑此页面”查看本文档的所有更改。
悬停查看原始许可证。