Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

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

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

      Returns a pointer to a null-terminated character array with data equivalent to those stored in the string.

      The pointer is such that the range[c_str()c_str()+ size()] is valid and the values in it correspond to the values stored in the string with an additional null character after the last position.

      The pointer obtained fromc_str() may be invalidated by:

      Writing to the character array accessed throughc_str() is undefined behavior.

      c_str() anddata() perform the same function.

      (since C++11)

      Contents

      [edit]Parameters

      (none)

      [edit]Return value

      Pointer to the underlying character storage.

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

      (until C++11)

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

      (since C++11)

      [edit]Complexity

      Constant.

      [edit]Notes

      The pointer obtained fromc_str() may only be treated as a pointer to a null-terminated character string if the string object does not contain other null characters.

      [edit]Example

      Run this code
      #include <algorithm>#include <cassert>#include <cstring>#include <string> extern"C"void c_func(constchar* c_str){    printf("c_func called with '%s'\n", c_str);} int main(){std::stringconst s("Emplary");constchar* p= s.c_str();assert(s.size()==std::strlen(p));assert(std::equal(s.begin(), s.end(), p));assert(std::equal(p, p+ s.size(), s.begin()));assert('\0'==*(p+ s.size()));     c_func(s.c_str());}

      Output:

      c_func called with 'Emplary'

      [edit]See also

      (DR*)
      accesses the first character
      (public member function)[edit]
      (DR*)
      accesses the last character
      (public member function)[edit]
      returns a pointer to the first character of a string
      (public member function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/string/basic_string/c_str&oldid=177144"

      [8]ページ先頭

      ©2009-2025 Movatter.jp