提供了push_back()成員函數來追加字符。將字符c追加到字符串的末尾,將其長度增加一。句法:
void string::push_back (char c) 參數: Character which to be appended. Return value:None Error: throws length_error if the resulting size exceeds the maximum number of characters(max_size).
// CPP code for to illustrate
// std::string::push_back()
#include <iostream>
#include <string>
using namespace std;
// Function to demonstrate push_back()
void push_backDemo(string str1, string str2)
{
// Appends character by character str2
// at the end of str1
for(int i = 0; str2[i] != '\0'; i++)
{
str1.push_back(str2[i]);
}
cout << "After push_back:";
cout << str1;
}
// Driver code
int main()
{
string str1("Geeksfor");
string str2("Geeks");
cout << "Original String:" << str1 << endl;
push_backDemo(str1, str2);
return 0;
}
輸出:
Original String:Geeksfor After push_back:GeeksforGeeks
相關用法
注:本文由純淨天空篩選整理自 std::string::push_back() in C++。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。