复制源字符串中索引位置起的特定数量的字符到目标字符数组。
此方法有可能,不安全,因为它依赖于调用方检查传递的值是正确的。 请考虑改用 basic_string::_Copy_s。
size_type copy(
value_type* _Ptr,
size_type _Count,
size_type _Off = 0
) const;
参数
_Ptr
将元素复制的目标字符数组。_ Count
从源字符串要复制的,至多,字符数。_Off
在复制将创建的源字符串的开头位置。
返回值
实际上要复制的字符数。
备注
空字符复制不追加到的末尾。
示例
// basic_string_copy.cpp
// compile with: /EHsc /W3
#include <string>
#include <iostream>
int main( )
{
using namespace std;
string str1 ( "Hello World" );
basic_string <char>::iterator str_Iter;
char array1 [ 20 ] = { 0 };
char array2 [ 10 ] = { 0 };
basic_string <char>:: pointer array1Ptr = array1;
basic_string <char>:: value_type *array2Ptr = array2;
cout << "The original string str1 is: ";
for ( str_Iter = str1.begin( ); str_Iter != str1.end( ); str_Iter++ )
cout << *str_Iter;
cout << endl;
basic_string <char>:: size_type nArray1;
// Note: string::copy is potentially unsafe, consider
// using string::_Copy_s instead.
nArray1 = str1.copy ( array1Ptr , 12 ); // C4996
cout << "The number of copied characters in array1 is: "
<< nArray1 << endl;
cout << "The copied characters array1 is: " << array1 << endl;
basic_string <char>:: size_type nArray2;
// Note: string::copy is potentially unsafe, consider
// using string::_Copy_s instead.
nArray2 = str1.copy ( array2Ptr , 5 , 6 ); // C4996
cout << "The number of copied characters in array2 is: "
<< nArray2 << endl;
cout << "The copied characters array2 is: " << array2Ptr << endl;
}
要求
标头:< 字符串>
命名空间: std