c++ std::ostringstream vs std::string::append

前端 未结 4 1322
难免孤独
难免孤独 2020-12-13 06:16

In all examples that use some kind of buffering I see they use stream instead of string. How is std::ostringstream and << operator different than using string.append.

4条回答
  •  南方客
    南方客 (楼主)
    2020-12-13 06:43

    With stream you can have your class Myclass override the << operation so that you can write

    MyClass x;
    ostringstream y;
    y << x;
    

    For append you need to have a ToString method (or something similar) since you can't override the append function of string.

    For some code pieces use whatever you feel more comfortable with. Use stream for bigger projects where it's useful to be able to simply stream an object.

提交回复
热议问题