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

Elm*_*dov 5 c++ string input scanf

我可以读一个字符串,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];
  }
}
Run Code Online (Sandbox Code Playgroud)

Gre*_*ill 10

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

char tmp[101];
scanf("%100s", tmp);
s[i][j] = tmp;
Run Code Online (Sandbox Code Playgroud)

  • noob问题:如果您使用"%100s",当您输入少于100个字符的字符串时它会存储什么?(因为它被告知存储100个字符) (2认同)