Namespaces
Variants

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

From cppreference.net
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 未提供异常安全保证 增加强异常安全保证

参见

( DR* )
移除末尾字符
(公开成员函数)