Translations of this page?:

copy

Syntax:

    #include <string>
    size_type copy( charT* str, size_type num, size_type index = 0 ) const;

copy()関数は、strへ現在の文字列からnum文字分をコピーします。(indexが指定されている場合は、index位置から始まり、指定されていない場合は、”0”からです。) copy()関数の返り値は、コピーされた文字数です。 例えば、次のコードは、文字列の部分文字列を抜き出し、char型の配列へコピーするために、copy()関数を利用しています。

   char buf[30];
   memset( buf, '\0', 30 );
   string str = "Trying is the first step towards success.";
   str.copy( buf, 24 );
   cout << buf << endl;

実行結果

   Trying is the first step

覚書 copy()関数を、呼び出す前に、最初にmemset()で、配列をNULL文字で埋めておく必要があります。 copy()関数の返り値は、NULL文字で終端されている必要があるためです。

Related Topics: substr

 
• • • SitemapRecent changesRSScc