std::basic_string::copy - cppreference.com

##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# std::basic_string::copy

From cppreference.com
< cpp‎ | string‎ | basic string

size_type copy( CharT* dest, size_type count, size_type pos = 0) const; |  |

Copies a substring `[pos, pos+count)` to character string pointed to by `dest`. If the requested substring lasts past the end of the string, or if count == npos, the copied substring is `[pos, size())`. The resulting character string is not null-terminated.

If pos >= size(), std::out_of_range is thrown.

### [edit] Parameters

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

### [edit] Return value

number of characters copied

### [edit] Exceptions

std::out_of_range if pos >= size().

### [edit] Complexity

linear in `count`

### [edit] Example

Run this code

    #include <string>
    #include <iostream>

    int main()
    {
      std::string foo("quuuux");
      char bar[7];
      foo.copy(bar, sizeof bar);
      bar[6] = '\0';
      std::cout << bar << '\n';
    }

Output:

    quuuux

### [edit] See also

substr | returns a substring (public member function) [edit]

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=cpp/string/basic_string/copy&oldid=72010"