std::string push_back() 方法
- 自 C++20 起
- 直到 C++20
constexpr void push_back( CharT ch );
// Non const version only
void push_back( CharT ch );
将给定字符 ch 附加到字符串的末尾。
参数
ch
- 要附加的字符
返回值
(无)
复杂度
分摊常数 - O(1)。
异常
如果操作导致 size() > max_size()
,则抛出std::length_error
。
示例
Main.cpp
#include <cassert>
#include <string>
#include <iomanip>
#include <iostream>
int main()
{
std::string str{"Short string"};
std::cout << "before=" << std::quoted(str) << '\n';
assert(str.size() == 12);
str.push_back('!');
std::cout << " after=" << quoted(str) << '\n';
assert(str.size() == 13);
}
输出
before="Short string"
after="Short string!"