basic_string::copy

コピー元の文字列のインデックス位置からターゲットの文字配列に最大で指定された文字数をコピーします。

このメソッドは、渡された値が正しいことを確認するために、呼び出し元に依存しているため、その安全でないです。 代わりに 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

参照

関連項目

basic_string クラス