std::basic_string_view<CharT,Traits>::data
From cppreference.com
< cpp | string | basic string view
| constexpr const_pointer data() const noexcept; |
(C++17以降) | |
基となる文字配列へのポインタを返します。返されるポインタは、範囲 [data(), data() + size()) が有効であり、その中の値がビューの値に対応するようなものです。
目次 |
[編集] パラメータ
(なし)
[編集] 返り値
基となる文字配列へのポインタ。
[編集] 複雑性
定数。
[編集] 注記
std::basic_string::data() や文字列リテラルとは異なり、std::basic_string_view::data() は、例えば部分文字列ビュー(remove_suffix などから)のように、必ずしもヌル終端されていないバッファへのポインタを返します。したがって、ヌル終端された文字列を期待する const CharT* のみを引数にとるルーチンに data() を渡すことは、通常は誤りです。
[編集] 例
このコードを実行
#include <cstring> #include <cwchar> #include <iostream> #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 }
出力
5 3
[編集] 関連項目
| 最初の文字にアクセスする (public member function) | |
| 最後の文字にアクセスする (public member function) | |
| 文字列の最初の文字へのポインタを返す ( std::basic_string<CharT,Traits,Allocator> のメンバ関数) |