读入
首先利用resize()给string分配空间。
然后再利用scanf()函数读入
输出
利用c_str()函数输出。
样例代码
#include <cstdio>
#include <string>
int main() {
std::string str;
str.resize(50);//这个必须有,很重要。
scanf("%s", &str[0]);
printf("%s\n", str.c_str());
return 0;
}
C++与字符串读写技巧
首先利用resize()给string分配空间。
然后再利用scanf()函数读入
利用c_str()函数输出。
#include <cstdio>
#include <string>
int main() {
std::string str;
str.resize(50);//这个必须有,很重要。
scanf("%s", &str[0]);
printf("%s\n", str.c_str());
return 0;
}

被折叠的 条评论
为什么被折叠?