名前空間
変種
操作

std::basic_string_view<CharT,Traits>::data

提供: cppreference.com
 
 
 
 
constexpr const_pointer data() const noexcept;
(C++17以上)

ベースとなる文字配列を指すポインタを返します。 ポインタは範囲 [data(); data() + size()) が有効であり、その中の値はビューの値に対応しています。

目次

[編集] 引数

(なし)

[編集] 戻り値

ベースとなる文字配列を指すポインタ。

[編集] 計算量

一定。

[編集] ノート

std::basic_string::data() や文字列リテラルと異なり、 data() はヌル終端されていないバッファを指すポインタを返す場合があります。 そのため、 const CharT* だけを取りヌル終端文字列を期待するルーチンに data() を渡すことは一般的には間違いです。

[編集]

#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

[編集] 関連項目

最初の文字にアクセスします
(パブリックメンバ関数) [edit]
最後の文字にアクセスします
(パブリックメンバ関数) [edit]