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

std::basic_string_view::data

constexpr const_pointer data() const;

(since C++17)

返回指向基础字符数组的指针。指针的范围。[数据%28%29;数据%28%29+大小%28%29%29是有效的,其中的值对应于视图的值。

参数

%280%29

返回值

指向基础字符数组的指针。

例外

noexcept规格:

noexcept

复杂性

常量。

注记

不像std::basic_string::data()和字符串文字,data()可能会返回指向非空终止缓冲区的指针。因此,这通常是一个错误的通过。data()一个只需要一个const CharT*并期望一个以空结尾的字符串。

二次

代码语言:javascript
复制
#include <iostream>
#include <cstring>
#include <cwchar>
#include <string>
#include <string_view>
int main()
{
    std::wstring_view wcstr_v = L"xyzzy";
    std::cout << std::wcslen(wcstr_v.data()) << '\n';
    // OK: the underlying character array is null-terminated
 
    char array[3] = {'B', 'a', 'r'};
    std::string_view array_v(array, sizeof array);
    // std::cout << std::strlen(array_v.data()) << '\n';
    // error: the underlying character array is not null-terminated
 
    std::string str(array_v.data(), array_v.size()); // OK
    std::cout << std::strlen(str.data()) << '\n';
    // OK: the underlying character array of a std::string is always null-terminated
}

二次

产出:

二次

代码语言:javascript
复制
5
3

二次

另见

front

accesses the first character (public member function)

back

accesses the last character (public member function)

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

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

扫码关注腾讯云开发者

领取腾讯云代金券