此文来自: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.
本文详细介绍了 C++ 中 string 类的 append 方法的多种用法,包括如何在字符串末尾追加子串、指定数量的字符等,并通过实例展示了如何使用这些方法。
1667

被折叠的 条评论
为什么被折叠?



