正文

c++中的string用法(一)2006-07-27 20:31:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/tonyhu/16880.html

分享到:

basic_string::append

string 的后面加字符或字符串。(+=, push_back 更灵活)

(1)string 的后面加C-string

basic_string& append( const value_type* _Ptr );

string s ( "Hello " ); // s=Hello

const char *c = "Out There ";

s.append ( c ); // s=Hello Out There

(2)string 的后面加C-string 的一部分

basic_string& append( const value_type* _Ptr, size_type _Count );

string s ( "Hello " ); // s=Hello

const char *c = "Out There ";

s.append ( c , 3 ); // s=Hello Out

(3)string 的后面加string(有两种方法)

basic_string& append( const basic_string& _Str );

string s1 ( "Hello " ), s2 ( "Wide " ), s3( "World " );

s1.append ( s2 ); // s1=Hello Wide

s1 += s3; // s1=Hello Wide World

(4)string 的后面加string 的一部分 ---A

basic_string& append( const basic_string& _Str, size_type _Off,

size_type _Count );

string s1 ( "Hello " ), s2 ( "Wide World " );

s1.append ( s2 , 5 , 5 ); // s1=Hello World

(5)string 的后面加string 的一部分 ---B

template<class InputIterator> basic_string& append(

InputIterator _First, InputIterator _Last );

string str1f ( "Hello " ), str2f ( "Wide World" );

str1f.append ( str2f.begin ( ) + 5 , str2f.end ( ) );

// s1=Hello World

(6)string 的后面加多个字符

basic_string& append( size_type _Count, value_type _Ch );

string str1e ( "Hello " );

str1e.append ( 4 , '!' ); // s1=Hello !!!!

basic_string::assign

string 赋值。 (比“=”更灵活)

(1)string C-string

basic_string& assign( const value_type* _Ptr );

string s;

const char *c = "Out There";

s.assign ( c ); // s=Out There

(2)string C-string 的一部分

basic_string& assign( const value_type* _Ptr, size_type _Count );

string s;

const char *c = "Out There";

s.assign ( c , 3 ); // s=Out

(3)string string(有两种方法)

basic_string& assign( const basic_string& _Str );

string s1 ( "Hello" ), s2 ( "Wide" ), s3( "World" );

s1.assign ( s2 ); // s1=Wide

s1 = s3; // s1=World

(4)string string 的一部分 ---A

basic_string& assign( const basic_string& _Str, size_type off,

size_type _Count );

string s1 ( "Hello " ), s2 ( "Wide World " );

s1.assign ( s2 , 5 , 5 ); // s1=Hello World

(5)string string 的一部分 ---B

template<class InIt> basic_string& assign(

InputIterator _First,

InputIterator _Last );

string str1f ( "Hello " ), str2f ( "Wide World" );

str1f.assign ( str2f.begin ( ) + 5 , str2f.end ( ) ); // s1=Wide World

(6)string 赋 多个字符

basic_string& assign( size_type _Count, value_type _Ch );

string str1e ( "Hello " );

str1e.assign ( 4 , '!' ); // s1=!!!!

阅读(22092) | 评论(3)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

loading...
您需要登录后才能评论,请 登录 或者 注册