C++之std::string ------ append用法

语法1:附加字符串str的字符。如果结果大小超过最大字符数,它将抛出length_error。

string& string::append (const string& str)

str: is the string to be appended.
返回: *this

例子:

#include <iostream> 
#include <string> 
using namespace std; 
   
// Function to demonstrate append() 
void appendDemo(string str1, string str2) 
{ 
    // Appends str2 in str1 
    str1.append(str2); 
    cout << "Using append():"; 
    cout << str1 << endl; 
} 
   
// Driver code 
int main() 
{ 
    string str1("Hello World! "); 
    string str2("GeeksforGeeks"); 
   
    cout << "Original String:" << str1 << endl; 
    appendDemo(str1, str2); 
   
    return 0; 
}

输出
Original String:Hello World! 
Using append():Hello World! GeeksforGeeks

 语法2:最多附加字符串str的str_num字符,从索引str_idx开始。如果str_idx> str,则抛出out_of_range。 size()。如果结果大小超过最大字符数,它将抛出length_error。

string& string::append (const string& str, size_type str_idx, size_type str_num)

str: is the string to be appended
str_num: being number of characters
str_idx: is index number.
返回:*this.

 例子:

#include <iostream> 
#include <string> 
using namespace std; 
   
// Function to demonstrate append() 
void appendDemo(string str1, string str2) 
{ 
    // Appends 5 characters from 0th index of 
    // str2 to str1 
    str1.append(str2, 0, 5); 
    cout << "Using append():"; 
    cout << str1; 
} 
   
// Driver code 
int main() 
{ 
    string str1("GeeksforGeeks "); 
    string str2("Hello World! "); 
   
    cout << "Original String:" << str1 << endl; 
    appendDemo(str1, str2); 
   
    return 0; 
}
输出:

Original String:GeeksforGeeks 
Using append():GeeksforGeeks Hello

语法3:追加字符数组chars的chars_len字符。如果结果大小超过最大字符数,则抛出length_error。

string& string::append (const char* chars, size_type chars_len)

*chars is the pointer to character array to be appended.
chrs_len: is the number of characters from *chars to be appended.
Note that chars must have at least chars_len characters. 
返回: *this.

例子:

#include <iostream> 
#include <string> 
using namespace std; 
   
// Function to demonstrate append 
void appendDemo(string str) 
{ 
    // Appends 5 characters from "GeeksforGeeks" 
    // to str 
    str.append("GeeksforGeeks", 5); 
    cout << "Using append():"; 
    cout << str << endl; 
} 
   
// Driver code 
int main() 
{ 
    string str("World of "); 
   
    cout << "Original String:" << str << endl; 
    appendDemo(str); 
   
    return 0; 
}

输出:

Original String:World of 
Using append():World of Geeks

语法4:追加字符c的出现次数。如果结果大小超过最大字符数,则抛出length_error。

string& string::append (size_type num, char c)

num: is the number of occurrences
c:is the character which is to be appended repeatedly. 
返回: *this.

 例子:

#include <iostream> 
#include <string> 
using namespace std; 
    
// Function to demonstrate append 
void appendDemo(string str) 
{ 
    // Appends 10 occurrences of '$' 
    // to str 
    str.append(10, '$'); 
    cout << "After append():"; 
    cout << str; 
   
} 
           
// Driver code 
int main() 
{ 
    string str("#########"); 
   
    cout << "Original String:" << str << endl; 
    appendDemo(str); 
    
    return 0; 
}
输出:

Original String:#########
After append():#########$$$$$$$$$$

语法5:追加范围[beg,end)的所有字符。如果结果大小超过最大字符数,则抛出length_error

string& string::append (InputIterator beg, InputIterator end)

first, last:Input iterators to the initial and final positions 
in a sequence.
返回 *this.

例子:

#include <iostream> 
#include <string> 
using namespace std; 
   
// Function to demonstrate append 
void appendDemo(string str1, string str2) 
{ 
   
    // Appends all characters from 
    // str2.begin()+5, str2.end() to str1 
    str1.append(str2.begin() + 5, str2.end()); 
    cout << "Using append:"; 
    cout << str1; 
} 
// Driver code 
int main() 
{ 
    string str1("Hello World! "); 
    string str2("GeeksforGeeks"); 
   
    cout << "Original String:" << str1 << endl; 
    appendDemo(str1, str2); 
   
    return 0; 
}

输出:

Original String:Hello World! 
Using append:Hello World! forGeeks

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yefei123123

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值