码迷,mamicode.com
首页 > 其他好文 > 详细

用scanf、printf输入输出string型字符串

时间:2020-04-26 21:23:38      阅读:113      评论:0      收藏:0      [点我收藏+]

标签:图片   style   成员   mes   ima   pac   一点   scan   str1   

    c语言里是没有string型的,string在c++里面。有的时候在c++里要用scanf、printf输入输出string型字符串,这是可以实现的,不过要做一点处理。

具体操作看代码:

#include<cstdio>
#include<string>
using namespace std;
int main()
{
    int n;
    string str1;
    scanf("%d",&n);
    str1.resize(n);  //给字符串str1预留足够的空间
    scanf("%s",&str1[0]);  //不能用&str1
    printf("%s\n",str1.c_str());  //用str1自带的成员函数c_str()把c++里的string型字符串转换成c字符串
    return 0;
}

结果如下:

技术图片

用scanf、printf输入输出string型字符串

标签:图片   style   成员   mes   ima   pac   一点   scan   str1   

原文地址:https://www.cnblogs.com/buanxu/p/12781937.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!