string::append

此文来自:http://huycwork.blog.163.com/blog/static/13675199920103122325012/

 

 

原型:

 #include <string>
 string
& append( const string& str );
 string
& append( const Char* str );
 string
& append( const string& str, size_type index, size_type len );
 string
& append( const Char* str, size_type num );
 string
& append( size_type num, Char ch );
 string
& append( input_iterator start, input_iterator end );

函数append用于:

  • (1&2)在当前字符串末尾追加 ,
  • (3)在当前字符串末尾追加 str的子串, 字串由index指定 开始位置,  len指定字符个数 ,
  • (4)追加str中的num个前导字符到字符串
  • (5)在当前字符串末尾追加 ch的副本 ,
  • (6)在当前字符串末尾追加由迭代器 end指定的序列 .

例如, 下面的代码追加10份'!'的拷贝到字符串末尾:

 string str = "Hello World";
 str.
append( 10, '!' );
 cout << str << endl;

代码输出:

 Hello World!!!!!!!!!!

下一个范例使用append()在一个字符串中追加另外一个字符串的子串:

 string str1 = "Eventually I stopped caring... ";
 
string str2 = "but that was the '80s so nobody noticed.";
   str1.append( str2, 25, 15 );
 
cout << "str1 is " << str1 << endl;

运行之后显示:

 str1 is Eventually I stopped caring... nobody noticed.

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值