public static member function
<string>

std::char_traits::copy

static char_type* copy (char_type* dest, const char_type* src, size_t n);
Copy character sequence
Copies the sequence of n characters pointed by src to the array pointed by dest. Ranges shall not overlap.

All character traits types shall implement the function as if the individual characters were assigned using member assign.

Parameters

dest
Pointer to an array where the copied characters are written.
src
Pointer to an array with the n characters to copy.
n
Number of characters to copy.

Notice that the function will consider that the length of both dest and src sequences is n characters, independently on whether any of them contains null-characters.
Member type char_type is the character type (i.e., the class template parameter in char_traits).
size_t is an unsigned integral type.

Return Value

Returns dest.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// char_traits::copy
#include <iostream>   // std::cout
#include <string>     // std::char_traits

int main ()
{
  char foo[] = "test string";
  char bar[20];

  unsigned len = std::char_traits<char>::length(foo);
  std::char_traits<char>::copy (bar,foo,len);

  bar[len] = '\0'; // append null-character

  std::cout << "foo contains: " << foo << '\n';
  std::cout << "bar contains: " << bar << '\n';
  return 0;
}

Output:
foo contains: test string
bar contains: test string


Complexity

Linear in n.

Exception safety

Unless either dest or src does not point to an array long enough, this member function never throws exceptions (no-throw guarantee) in any of the standard specializations.
Otherwise, it causes undefined behavior.

See also