The easiest way to read formatted input in C++?

后端 未结 4 962
醉酒成梦
醉酒成梦 2021-01-17 08:37

Is there any way to read a formatted string like this, for example :48754+7812=Abcs.

Let\'s say I have three stringz X,Y and Z, and I want



        
4条回答
  •  没有蜡笔的小新
    2021-01-17 09:06

    You can use scanf. It is not overly C++ - ish, but it does the trick with remarkably few lines of code:

    char a[101], b[111], c[121];
    sscanf(":48754+7812=Abcs", ":%100[^+]+%110[^=]=%120s", a, b, c);
    string sa(a), sb(b), sc(c);
    cout << sa << "-" << sb  << "-" << sc << endl;
    

    The idea is to specify the characters accepted by the strings that you read using a very limited regular expression syntax. In this case, the first string is read up to the plus, and the second string is read up to the equals sign.

提交回复
热议问题