Home C++ Strings Functions copy() C++ std::string copy() Function 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.
char buf[16];
std::size_t n = s.copy(buf, 3, 0);
buf[n] = '\\0'; Call What 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 size_type copy(char* dest, size_type count, size_type pos = 0) const; Parameters Parameter Description 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 Value Description size_typeNumber of characters actually copied.
C++ Examples 1. Copy three chars 2. Copy from pos 3. Fewer available Example 1 — Basic copy() Copy a prefix into a buffer and terminate it.
#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 We request up to four characters from the start. copy writes C,o,d,e and returns 4.We add '\\0' so printing as a C-string is safe. Example 2 — Start at a Position Copy from the middle of the string.
#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 Start index 2 points at 'c'. Three characters c,d,e are copied. Null termination makes buf printable. Example 3 — Copy Stops at End Ask for more characters than remain.
#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 We asked for 10 characters, but only 2 exist. copy copies what is available and returns 2.Always trust the return value for the real count. Frequently Asked Questions What does copy() do? It copies characters from the std::string into a char buffer you provide.
Does copy() add a null terminator? No. Add '\0' yourself if you need a C-string.
What does copy() return? The number of characters actually copied.
What if pos is past the end? copy throws std::out_of_range when pos > size().
When should I use copy() instead of c_str()? 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.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion