Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::basic_string<CharT,Traits,Allocator>::data

      From cppreference.com
      <cpp‎ |string‎ |basic string
       
       
       
      std::basic_string
       
      const CharT* data()const;
      (1)(noexcept since C++11)
      (constexpr since C++20)
      CharT* data()noexcept;
      (2)(since C++17)
      (constexpr since C++20)

      Returns a pointer to the underlying array serving as character storage. The pointer is such that the range

      [data()data()+ size())

      (until C++11)

      [data()data()+ size()]

      (since C++11)

      is valid and the values in it correspond to the values stored in the string.

      The returned array is not required to be null-terminated.

      Ifempty() returnstrue, the pointer is a non-null pointer that should not be dereferenced.

      (until C++11)

      The returned array is null-terminated, that is,data() andc_str() perform the same function.

      Ifempty() returnstrue, the pointer points to a single null character.

      (since C++11)

      The pointer obtained fromdata() may be invalidated by:

      1) Modifying the character array accessed through the const overload ofdata has undefined behavior.
      2) Modifying the past-the-end null terminator stored atdata() +size() to any value other thanCharT() has undefined behavior.

      Contents

      [edit]Parameters

      (none)

      [edit]Return value

      A pointer to the underlying character storage.

      data()[i]== operator[](i) for everyi in[0size()).

      (until C++11)

      data()+ i==std::addressof(operator[](i)) for everyi in[0size()].

      (since C++11)

      [edit]Complexity

      Constant.

      [edit]Example

      Run this code
      #include <algorithm>#include <cassert>#include <cstring>#include <string> int main(){std::stringconst 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*)
      accesses the first character
      (public member function)[edit]
      (DR*)
      accesses the last character
      (public member function)[edit]
      returns a non-modifiable standard C character array version of the string
      (public member function)[edit]
      returns a pointer to the first character of a view
      (public member function ofstd::basic_string_view<CharT,Traits>)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/string/basic_string/data&oldid=171123"

      [8]ページ先頭

      ©2009-2025 Movatter.jp