gpt4 book ai didi

c++ - 如何在 C++ 中使用 scanf() 读取字符串?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:56:51 26 4
gpt4 key购买 nike

我可以使用 std::cin 读取字符串,但我不知道如何使用 withscanf() 读取字符串。如何更改下面的代码以使用 scanf() ?

string s[20][5];

for (int i=1;i<=10;i++)
{
for (int j=1;j<=3;j++)
{
cin>>s[i][j];
}
}

最佳答案

使用 C scanf() 函数需要使用 C 字符串。此示例使用临时 C 字符串 tmp,然后将数据复制到目标 std::string

char tmp[101];
scanf("%100s", tmp);
s[i][j] = tmp;

关于c++ - 如何在 C++ 中使用 scanf() 读取字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5910483/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com