当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C++ std::string::push_back()用法及代码示例


提供了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++。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。