文字列コンストラクタ

提供:cppreference.com
移動: 案内, 検索

文法:

    #include <string>
    string();
    string( const string& s );
    string( size_type length, charT ch );
    string( const charT* str );
    string( const charT* str, size_type length );
    string( const string& str, size_type index, size_type length );
    string( input_iterator start, input_iterator end );
    ~string();

新しい文字列を以下のような様々な方法で構築する:

  • 引数なし; 空の文字列
  • 与えられた文字列sのコピー,
  • 文字 chをlength分コピー,
  • strを複製する (オプションで複製する文字列の長さ を指定できる),
  • 他の文字列 のインデックス から始まり、長さ の大きさを持つ部分文字列の複製をする
  • コピー元の最初のイテレータ からコピー元の最後のイテレータ の間の要素をコピーする

例,

     string str1( 5, 'c' );
     string str2( "Now is the time..." );
     string str3( str2, 11, 4 );
     cout << str1 << endl;
     cout << str2 << endl;
     cout << str3 << endl;

表示

     ccccc
     Now is the time...
     time

The string constructors usually run in linear time, except the empty constructor, which runs in constant time.

個人用ツール
名前空間
変種
操作
案内
ツールボックス
他の言語