📜  C++中的std :: 字符串:: push_back()

📅  最后修改于: 2021-05-30 20:15:00             🧑  作者: Mango

提供了push_back()成员函数以追加字符。将字符c追加到字符串的末尾,将其长度增加一。
句法 :

void string:: push_back (char c)
Parameters:  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 
#include 
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
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”