What exactly does stringstream do?

前端 未结 4 616
谎友^
谎友^ 2020-11-28 01:05

I am trying to learn C++ since yesterday and I am using this document:http://www.cplusplus.com/files/tutorial.pdf (page 32) . I found a code in the document and I ran it. I

4条回答
  •  伪装坚强ぢ
    2020-11-28 01:47

    Sometimes it is very convenient to use stringstream to convert between strings and other numerical types. The usage of stringstream is similar to the usage of iostream, so it is not a burden to learn.

    Stringstreams can be used to both read strings and write data into strings. It mainly functions with a string buffer, but without a real I/O channel.

    The basic member functions of stringstream class are

    • str(), which returns the contents of its buffer in string type.

    • str(string), which set the contents of the buffer to the string argument.

    Here is an example of how to use string streams.

    ostringstream os;
    os << "dec: " << 15 << " hex: " << std::hex << 15 << endl;
    cout << os.str() << endl;
    

    The result is dec: 15 hex: f.

    istringstream is of more or less the same usage.

    To summarize, stringstream is a convenient way to manipulate strings like an independent I/O device.

    FYI, the inheritance relationships between the classes are:

    string stream classes

提交回复
热议问题