C++如何使用scanf、printf输入输出string类型的变量

版权声明:假装有个原创声明……虽然少许博文不属于完全原创,但也是自己辛辛苦苦总结的,转载请注明出处,感谢! https://blog.csdn.net/m0_37454852/article/details/86643319

只要在变量名后加.c_str()即可将string类型等效为C中的字符数组类型进行输入输出。

#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

int main()
{
    string str;
    scanf("%s", str.c_str());
    printf("%s", str.c_str());
    return 0;
}

运行效果

猜你喜欢

转载自blog.csdn.net/m0_37454852/article/details/86643319