std::basic_string<CharT,Traits,Allocator>::data
From cppreference.com
< cpp | string | basic string
| const CharT* data() const; |
(1) | (C++11 以降 noexcept) (C++20 以降 constexpr) |
| CharT* data() noexcept; |
(2) | (C++17以降) (C++20 以降 constexpr) |
基盤となる文字格納領域へのポインタを返します。このポインタは、範囲
|
|
(C++11まで) |
|
|
(C++11以降) |
が有効であり、その中の値が文字列に格納されている値に対応しているようなポインタです。
|
返される配列はヌル終端である必要はありません。 If empty() returns true, the pointer is a non-null pointer that should not be dereferenced.(empty()がtrueを返す場合、ポインタはヌルポインタではなく、デリファレンスしてはなりません。) |
(C++11まで) |
|
返される配列はヌル終端です。つまり、 If empty() returns true, the pointer points to a single null character.(empty()がtrueを返す場合、ポインタは単一のヌル文字を指します。) |
(C++11以降) |
data() から取得したポインタは、以下によって無効になる可能性があります。
- Passing a non-const reference to the string to any standard library function, or(文字列への非const参照を標準ライブラリ関数に渡す場合、または)
- Calling non-const member functions on the string, excluding
operator[](), at(), front(), back(), begin(), end(), rbegin(), rend().(operator[]()、at()、front()、back()、begin()、end()、rbegin()、rend()を除く、文字列に対する非constメンバ関数を呼び出す場合。)
1) `data`のconstオーバーロードを介してアクセスされる文字配列を変更すると、未定義の動作になります。
2) `data() + ` size() に格納されている終端ヌル文字以外を変更すると、未定義の動作になります。
目次 |
[edit] Parameters
(なし)
[edit] Return value
基盤となる文字格納領域へのポインタ。
|
data()[i] == operator[](i) for every |
(C++11まで) |
|
data() + i == std::addressof(operator[](i)) for every |
(C++11以降) |
[edit] Complexity
定数。
[edit] Example
このコードを実行
#include <algorithm> #include <cassert> #include <cstring> #include <string> int main() { std::string const s("Emplary"); assert(s.size() == std::strlen(s.data())); assert(std::equal(s.begin(), s.end(), s.data())); assert(std::equal(s.data(), s.data() + s.size(), s.begin())); assert('\0' == *(s.data() + s.size())); }
[edit] See also
| (DR*) |
最初の文字にアクセスする (public member function) |
| (DR*) |
最後の文字にアクセスする (public member function) |
| returns a non-modifiable standard C character array version of the string(文字列の変更不可能な標準C形式の文字配列バージョンを返します) (public member function) | |
| ビューの最初の文字へのポインタを返す (public member function of std::basic_string_view<CharT,Traits>) |