构造函数(Constructors)
语法:
string(); string( size_type length, char ch ); string( const char *str ); string( const char *str, size_type length ); string( string &str, size_type index, size_type length ); string( input_iterator start, input_iterator end ); |
字符串的构造函数创建一个新字符串,包括:
- 以length为长度的ch的拷贝(即length个ch)
- 以str为初值 (长度任意),
- 以index为索引开始的子串,长度为length, 或者
- 以从start到end的元素为初值.
例如,
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