copy
Syntax: #include <string> size_type copy( char* str, size_type num, size_type index = 0 ); The copy() function copies num characters of the current string (starting at index if it's specified, 0 otherwise) into str. The return value of copy() is the number of characters copied. For example, the following code uses copy() to extract a substring of a string into an array of characters: char buf[30]; memset( buf, '\0', 30 ); string str = "Trying is the first step towards failure."; str.copy( buf, 24 ); cout << buf << endl; When run, this code displays: Trying is the first step Note that before calling copy(), we first call (Standard C String and Character) memset() to fill the destination array with copies of the NULL character. This step is included to make sure that the resulting array of characters is NULL-terminated. Related topics:
|