C++ String 库 - resize
描述
它将字符串的大小调整为 n 个字符的长度。
声明
以下是 std::string::resize 的声明。
void resize (size_t n);
C++11
void resize (size_t n, char c);
参数
n − 这是一个新的字符串长度。
c − 用于填充添加到字符串中的新字符空间的字符。
返回值
none
异常
如果抛出异常,则字符串没有变化。
示例
在下面的 std::string::resize 示例中。
#include <iostream>
#include <string>
int main () {
std::string str ("Sairamkrishna Mammahe");
std::cout << str << '\n';
unsigned sz = str.size();
str.resize (sz+2,'+');
std::cout << str << '\n';
str.resize (14);
std::cout << str << '\n';
return 0;
}
示例输出应该是这样的 −
Sairamkrishna Mammahe Sairamkrishna Mammahe++ Sairamkrishna

