我可以使用 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/