Read C++ string with scanf

后端 未结 7 1088
离开以前
离开以前 2020-11-28 12:35

As the title said, I\'m curious if there is a way to read a C++ string with scanf.

I know that I can read each char and insert it in the deserved string, but I\'d wa

7条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-28 13:15

    int n=15; // you are going to scan no more than n symbols
    std::string str(n+1); //you can't scan more than string contains minus 1
    scanf("%s",str.begin()); // scanf only changes content of string like it's array
    str=str.c_str() //make string normal, you'll have lots of problems without this string
    

提交回复
热议问题