此函数用于将字符串的大小调整为k个字符的长度。
考虑一个字符串对象str。要调整字符串对象的大小,语法为:
str.resize(k,c);
该函数包含两个参数。
如果k小于字符串的长度,则字符串长度会缩短为k指定的长度,并删除k以外的所有字符。
如果k大于字符串的长度,则字符串长度将扩展到k指定的长度。
它不返回任何值。
链接:https://www.learnfk.comhttps://www.learnfk.com/c++/cpp-string-resize-function.html
来源:LearnFk无涯教程网
当k短于指定字符串的长度时。
#include<iostream> using namespace std; int main() { string str= "learnfk"; cout<<"String is :"<<str<<?\n?; str.resize(4); cout<<"After resizing, string is "<<str; return 0; }
当k大于指定字符串的长度时。
#include<iostream> using namespace std; int main() { string str ="learnfk"; cout<<"String value is :"<<str<<'\n'; str.resize(19,"tutorial"); cout<<"After resizing, string value is :"<<str; return 0; }
输出:
String value is learnfk After resizing, string value is learnfk tutorial
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)