basic_string::copy

复制至多指定数量的字符从一个索引位置的为目标字符数组的源字符串。

此方法是可能不安全的,因此,因为它依赖于调用方检查传递的值是否正确。 请考虑改用 basic_string::_Copy_s

size_type copy(
    value_type* _Ptr, 
    size_type _Count,
    size_type _Off = 0
) const;

参数

  • _Ptr
    组件将复制的目标字符数组。

  • _ Count
    从源字符串将复制的,至多,字符数。

  • _Off
    在复制将进行的源字符串的开始位置。

返回值

实际复制的字符数。

备注

null字符不追加到该副本的末尾。

示例

// 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;
}
  

要求

标头: <string>

命名空间: std

请参见

参考

basic_string Class