Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

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

      From cppreference.com
      <cpp‎ |string‎ |basic string
       
       
       
      std::basic_string
       
      size_type copy( CharT* dest, size_type count, size_type pos=0)const;
      (constexpr since C++20)

      Copies a substring[pospos+ count) to character string pointed to bydest. If the requested substring lasts past the end of the string, or ifcount== npos, the copied substring is[possize()).

      The resulting character string is not null-terminated.

      Contents

      [edit]Parameters

      dest - pointer to the destination character string
      count - length of the substring
      pos - position of the first character to include

      [edit]Return value

      Number of characters copied.

      [edit]Exceptions

      std::out_of_range ifpos> size().

      If an exception is thrown for any reason, this function has no effect (strong exception safety guarantee).

      [edit]Complexity

      Linear incount.

      [edit]Example

      Run this code
      #include <iostream>#include <string> int main(){std::string foo("WINE"); // brace-initialization initializes all characters to 0,// providing a null-terminatorchar bar[4]{}; // do not copy the last char, to guarantee null-termination    foo.copy(bar, sizeof bar-1); std::cout<< bar<<'\n';// requires bar to be null-terminated}

      Output:

      WIN

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      LWG 847C++98there was no exception safety guaranteeadded strong exception safety guarantee

      [edit]See also

      returns a substring
      (public member function)[edit]
      copies characters
      (public member function ofstd::basic_string_view<CharT,Traits>)[edit]
      copies a range of elements to a new location
      (function template)[edit]
      copies one buffer to another
      (function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/string/basic_string/copy&oldid=171090"

      [8]ページ先頭

      ©2009-2025 Movatter.jp