C++ String類的研究1: append 方法

1.append方法: 在指定string尾部添加字串。

append方法的返回的结果为新string的引用。

常用的有6種重載:

a. basic_string &append(const basic_string &str);

将basic_string 类型的参数添加到原string的末尾

舉例:

 string testappend = "Hello World";

testappend.append(" test");

cout<<testappend<<endl;

//打印结果为 Hello World test

 

b. basic_string &append( const char *str );

将char数组作为参数赋值给原string的末尾

举例:

string testappend = "Hello World";

char ch[10] = {' ', 't', 'e' ,'s' ,'t' ,'\0'};

testappend.append(ch);

cout<<testappend<<endl;

//打印结果为 Hello World test

 

c.basic_string &append( const basic_string &str, size_type index, size_type len );

将str字串从Index位置起,截取长度len,添加到原字串的末尾, index从1开始计算,而非0

举例:

string testappend = "Hello World";

testappend.append(" abcdefghijklmn", 6, 3);

cout<<testappend<<endl;

//打印结果为 Hello World efg

 

d. basic_string &append( const char *str, size_type num );

截取str字串首地址起的 num数量个字符,添加到原字串末尾

举例:

string testappend = "Hello World";

//直接使用string为第一个字串

testappend.append("???????????", 3);

//或者使用字符数组作为第一个参数,用str1或者&str1[0] 是相同的取数组首地址

char str1[20] = "1234567890";

testappend.append(str1, 6);

cout<<testappend<<endl;

//打印结果为 Hello World???123456

 

e. basic_string &append( size_type num, char ch );

将num数量的相同字符ch,添加到原字符串末尾

举例:

string testappend = "Hello World";

testappend.append(5, '5');

cout<<testappend<<endl;

//打印结果为 Hello World55555

 

6. basic_string &append( input_iterator start, input_iterator end );

把从iterator start开始的地方到iterator end结束的地方添加到原字符串的末尾

举例:

string testappend = "Hello World";

string it = "123456789";

testappend.append(it.begin()+5, it.end()-2);

cout<<testappend<<endl;

//打印结果是 Hello World67

--------------------------------------------------------------------------------

string& append ( const string& str );
string& append ( const string& str, size_t pos, size_t n );
string& append ( const char* s, size_t n );
string& append ( const char* s );
string& append ( size_t n, char c );
template <class InputIterator>
   string& append ( InputIterator first, InputIterator last );
Append to string

The current string content is extended by adding an additional appending string at its end.

The arguments passed to the function determine this appending string:

string& append ( const string& str );
Appends a copy of str.
string& append ( const string& str, size_t pos, size_t n );
Appends a copy of a substring of str. The substring is the portion of str that begins at the character position pos and takes up to n characters (it takes less than n if the end of string is reached before).
If the position passed is past the end of str, an out_of_range exception is thrown.
string& append ( const char * s, size_t n );
Appends a copy of the string formed by the first n characters in the array of characters pointed by s.
string& append ( const char * s );
Appends a copy of the string formed by the null-terminated character sequence (C string) pointed by s. The length of this character sequence is determined by the first ocurrence of a null character (as determined by traits.length(s)).
string& append ( size_t n, char c );
Appends a string formed by the repetition n times of character c.
template<class InputIterator> string& append (InputIterator first, InputIterator last);
If InputIterator is an integral type, behaves as the previous member function version, effectively appending a string formed by the repetition first times of the character equivalent to last.
In any other case, the parameters are considered iterators and the content is appended by the elements that go from element referred by iterator first to the element right before the one referred by iterator last.


 

Parameters

str
Another object of class string whose content is entirely or partially appended to the content of this.
pos
Starting position of the substring in str that is appended. Notice that the first position has a value of 0, not 1.
n
Number of characters to append.
s
Array with a sequence of characters.
In the third member version, the length is not determined by any occurrence of null-characters, but by parameter n.
By contrast, in the fourth member version, this is a null-terminated character sequence whose length is determined by the first occurrence of a null-character.
c
Character value, repeated n times.
start
If along with last, both are integers, it is equivalent to parameter n, otherwise it is an iterator referring to the beginning of a sequence of characters.
last
If along with start, both are integers, it is equivalent to parameter c, otherwise it is an iterator referring to the past-the-end element of a sequence of characters.


 

Return Value

*this

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// appending to string
#include <iostream>
#include <string>
using namespace std;

int main ()
{
  string str;
  string str2="Writing ";
  string str3="print 10 and then 5 more";

  // used in the same order as described above:
  str.append(str2);                       // "Writing "
  str.append(str3,6,3);                   // "10 "
  str.append("dots are cool",5);          // "dots "
  str.append("here: ");                   // "here: "
  str.append(10,'.');                     // ".........."
  str.append(str3.begin()+8,str3.end());  // " and then 5 more"
  str.append<int>(5,0x2E);                // "....."

  cout << str << endl;
  return 0;
}



Output:

Writing 10 dots here: .......... and then 5 more.....

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值