首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::basic_string_view::basic_string_view

constexpr basic_string_view();

(1)

(since C++17)

constexpr basic_string_view(const basic_string_view& other) = 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)

1%29默认构造函数。构造空basic_string_view.建造后,data()等于nullptr,和size()等于0...

2%29复制构造函数。构造与other.建造后,data()等于other.data(),和size()等于other.size()...

3%29构造第一个视图count字符数组的字符以s...s可以包含空字符。如果[s, s+count)不是有效的范围%28,即使构造函数不能访问此范围%29的任何元素。建造后,data()等于s,和size()等于count...

4%29构造以空结尾的字符串的视图。s,不包括终止空字符。视图的长度被确定为Traits::length(s)如果[s, s+Traits::length(s))不是有效范围。建造后,data()等于s,和size()等于Traits::length(s)...

参数

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

例外

1-3%29

noexcept规格:

noexcept

复杂性

1-3%29常数

4%29线性s

二次

代码语言:javascript
复制
#include <iostream>
#include <string>
#include <string_view>
int main()
{
    std::wstring_view wcstr_v = L"xyzzy";
 
    char array[3] = {'B', 'a', 'r'};
    std::string_view array_v(array, sizeof array);
 
    std::string cppstr = "Foo";
    std::string_view cppstr_v(&cppstr[0], cppstr.size()); 
 
    std::cout << cppstr_v << '\n'
              << array_v << '\n'
              << wcstr_v.size() << '\n';
}

二次

产出:

二次

代码语言:javascript
复制
Foo
Bar
5

二次

另见

operator=

assigns a view (public member function)

代码语言:txt
复制
 © cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券