首页 > 其他分享 >scanf,printf对string类型的处理

scanf,printf对string类型的处理

时间:2022-11-29 12:33:55浏览次数:42  
标签:string int scanf 110 printf cout

#include <bits/stdc++.h>
using namespace std;
/*
测试用例:
abc
*/
const int N = 110;
int main() {
    string a;

    // scanf读入string的方法
    a.resize(N); //需要预先分配空间
    scanf("%s", &a[0]);

    // printf输出string的方法
    printf("%s\n", a.c_str());

    //正确的字符串的长度:3
    cout << strlen(a.c_str()) << endl;

    //错误的字符串长度:110
    cout << a.size() << endl;

    return 0;
}

标签:string,int,scanf,110,printf,cout
From: https://www.cnblogs.com/littlehb/p/16935095.html

相关文章