名稱空間
變體
操作

std::basic_string<CharT,Traits,Allocator>::push_back

來自 cppreference.com
< cpp‎ | string‎ | basic_string
 
 
 
std::basic_string
 
void push_back( CharT ch );
(C++20 起為 constexpr)

將給定字元 ch 新增到字串的末尾。

目錄

[編輯] 引數

ch - 要新增的字元

[編輯] 返回值

(無)

[編輯] 複雜度

攤還常數時間。

[編輯] 異常

如果操作會導致 size() 超過 max_size(),則丟擲 std::length_error

如果由於任何原因丟擲異常,此函式無效果(強異常安全保證)。

[編輯] 示例

#include <iomanip>
#include <iostream>
#include <string>
 
int main()
{
    std::string str{"Short string"};
    std::cout << "1) " << std::quoted(str) << ", size: " << str.size() << '\n';
 
    str.push_back('!');
    std::cout << "2) " << std::quoted(str) << ", size: " << str.size() << '\n';
}

輸出

1) "Short string", size: 12
2) "Short string!", size: 13

[編輯] 缺陷報告

下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。

缺陷報告 應用於 釋出時的行為 正確的行為
LWG 7 C++98 1) C++ 標準中缺少描述
2) 引數型別為 const CharT
1) 添加了描述
2) 更改為 CharT
LWG 847 C++98 沒有異常安全保證 添加了強異常安全保證

[編輯] 另請參閱

移除最後一個字元
(公共成員函式) [編輯]