C++ std::string copy() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
std::string

Definition

In C++, std::string::copy() copies characters from the string into a buffer you own.

It returns how many characters were copied. It does not write a trailing '\\0'. Include <string>.

This is a member of std::string. A string grows as you append, stores characters contiguously, and can reallocate—after growth, old iterators and references may be invalid. Prefer at() when you want bounds checks; remember find returns npos when missing.

Syntax

Pass destination buffer, count, and optional start position.

C++
char buf[16];
std::size_t n = s.copy(buf, 3, 0);
buf[n] = '\\0';
CallWhat it does
s.copy(dest, count)Copy up to count chars from index 0 into dest.
s.copy(dest, count, pos)Same, but start at index pos.

If you need a C-string, add '\\0' yourself after copy, or use c_str() / strcpy patterns carefully with sized buffers.

Library declarations
C++
size_type copy(char* dest, size_type count, size_type pos = 0) const;

Parameters

ParameterDescription
destDestination buffer with room for the copied characters (and a null if you add one).
countMaximum characters to copy.
posOptional start index in the string. Throws if pos > size().

Return Value

Return ValueDescription
size_typeNumber of characters actually copied.

C++ Examples

Example 1 — Basic copy()

Copy a prefix into a buffer and terminate it.

C++
#include <iostream>
#include <string>

int main() {
    std::string s = "CodeToFun";
    char buf[8];
    std::size_t n = s.copy(buf, 4);
    buf[n] = '\\0';
    std::cout << buf << '\n';
    std::cout << "copied: " << n << '\n';
    return 0;
}

How It Works

  1. We request up to four characters from the start.
  2. copy writes C,o,d,e and returns 4.
  3. We add '\\0' so printing as a C-string is safe.

Example 2 — Start at a Position

Copy from the middle of the string.

C++
#include <iostream>
#include <string>

int main() {
    std::string s = "abcdef";
    char buf[8];
    std::size_t n = s.copy(buf, 3, 2);
    buf[n] = '\\0';
    std::cout << buf << '\n';
    return 0;
}

How It Works

  1. Start index 2 points at 'c'.
  2. Three characters c,d,e are copied.
  3. Null termination makes buf printable.

Example 3 — Copy Stops at End

Ask for more characters than remain.

C++
#include <iostream>
#include <string>

int main() {
    std::string s = "hi";
    char buf[8];
    std::size_t n = s.copy(buf, 10);
    buf[n] = '\\0';
    std::cout << "n=" << n << " text=" << buf << '\n';
    return 0;
}

How It Works

  1. We asked for 10 characters, but only 2 exist.
  2. copy copies what is available and returns 2.
  3. Always trust the return value for the real count.

Frequently Asked Questions

It copies characters from the std::string into a char buffer you provide.
No. Add '\0' yourself if you need a C-string.
The number of characters actually copied.
copy throws std::out_of_range when pos > size().
When you need a private mutable buffer, or only a substring, without exposing the string’s internal pointer.

Next: crbegin()

Keep going through the string functions. const reverse iterator to the last character.

crbegin() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful