共用方式為


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 類別