std::basic_string_view<CharT,Traits>::data
提供: cppreference.com
<tbody>
</tbody>
constexpr const_pointer data() const noexcept; |
(C++17以上) | |
ベースとなる文字配列を指すポインタを返します。 ポインタは範囲 [data(); data() + size()) が有効であり、その中の値はビューの値に対応しています。
引数
(なし)
戻り値
ベースとなる文字配列を指すポインタ。
計算量
一定。
ノート
std::basic_string::data() や文字列リテラルと異なり、 data() はヌル終端されていないバッファを指すポインタを返す場合があります。 そのため、 const CharT* だけを取りヌル終端文字列を期待するルーチンに data() を渡すことは一般的には間違いです。
例
Run this code
#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
}
出力:
5
3
関連項目
| 最初の文字にアクセスします (パブリックメンバ関数) | |
| 最後の文字にアクセスします (パブリックメンバ関数) |