Introduction to String Streams
A string stream is a stream that works with a string.
Why Strings Get Their Own Streams
A string stream is a stream that works with a string. Most streams you may have met connect to something outside the program, such as the keyboard, a file, or the screen. A string stream instead connects to a string, which is text held in memory. That makes it a convenient way to treat a piece of text as if it were a stream of values that can be read or written.
What do you think happens?
A program needs to assemble a message piece by piece and store the finished message inside a string. Which of the three string stream types would you expect to use, and why?
Reveal answer
ostringstream, because its name signals output and it is used for writing to a string.
The source describes ostringstream as the type for writing to a string, with data flowing from the program into the stream and the stream's contents being the result being assembled. Building a message inside a string is a writing task.
Three Names, Three Directions
A string stream is a stream attached to a string. The source describes three types of string stream: istringstream, ostringstream, and stringstream. Each type is named for the direction in which data moves.
The names do most of the teaching. The name istringstream signals input, and it is used for reading from a string. The name ostringstream signals output, and it is used for writing to a string. The name stringstream has no such prefix limiting it to one direction, and the source states that it is used for both reading from and writing to a string. Once you know what each name signals, you can tell at a glance whether a stream is meant for input, output, or both.
The three types are closely related rather than three unrelated tools. Each one is a stream attached to a string, and the only difference between them is which operations they support. It helps to learn them as one family with one small difference between members.
A useful way to picture this is to treat each stream as a channel with a direction. An istringstream carries data out of a string toward your program. An ostringstream carries data from your program into a string. A stringstream is a channel that can carry data in either direction, depending on which operations you use. The diagram below shows the three channels side by side.
Read the diagram from left to right in each row. The top row shows the reading direction: data moves from the string into the program through an istringstream. The middle row shows the writing direction: data moves from the program into a string through an ostringstream. The stringstream sits on its own because it is not limited to either direction. The source states that it supports both reading from and writing to a string, so the same object can take part in either flow.
Declaring Each Type
The source gives one declaration pattern for each type. In each pattern, s is the name you choose for the stream object. The part before s tells you which direction the stream supports.
- istringstream: the type name signals input; the stream is for reading from a string
- ostringstream: the type name signals output; the stream is for writing to a string
- stringstream: the declaration names both directions; the stream supports both operations
Notice the direction of data flow in each case. For istringstream, the text you want to pull values out of is the string the stream works on. For ostringstream, data flows into the stream, and the stream's contents are the result you are assembling. The stringstream is the only one of the three that the source describes as supporting both operations, which is why it is the most flexible choice when a task needs to move data in both directions.
| Type | Declaration pattern | Direction supported | Use it when |
|---|---|---|---|
| istringstream | istringstream s | Reading from a string | The task needs values taken out of a string |
| ostringstream | ostringstream s | Writing to a string | The task needs values assembled into a string |
| stringstream | stringstream s | Reading from and writing to a string | The task needs both directions |
The three string stream types and the direction each one supports
Tracing a Task Through the Right Stream
Picking a string stream type for a text-processing task
A program must take a line of text, pull values out of it, and then build a new summary string from some of those values. Which string stream type fits this task?
- 1Identify the reading part
The program takes values out of an existing line of text. That is reading from a string, which the source assigns to istringstream.
- 2Identify the writing part
The program builds a new summary string. That is writing to a string, which the source assigns to ostringstream.
- 3Decide whether one stream can cover both
The task needs data to move out of a string and into a string. The source describes stringstream as the type that supports both reading from and writing to a string, so it is the general-purpose choice when both directions are needed.
A stringstream fits this task because it needs both directions. If the task had only read values out of the text, istringstream would be the more precise choice.
Choosing the Right Type
When you are choosing a string stream type, the question to ask is which direction the task needs. Work through the decisions in order. First ask whether the task reads from a string. If it does, ask whether it also writes to that string. Reading and writing together point to stringstream, and reading alone points to istringstream. If the task does not read from a string, ask whether it writes to one. Writing alone points to ostringstream.
Mistakes to Avoid
Common mistake Treating the three types as three unrelated tools
Why it failsThe source presents them as closely related. Each is a stream attached to a string, and they differ only in which operations they support.
Do this insteadLearn them as one family and remember the single point of difference: the direction each type supports.
Common mistake Using ostringstream to read values out of a string
Why it failsThe name ostringstream signals output, and the source describes it for writing to a string, not reading from one.
Do this insteadUse istringstream when the task reads from a string, and reserve ostringstream for writing.
Common mistake Always defaulting to stringstream
Why it failsstringstream is the general-purpose choice when both directions are needed. Choosing it when the task only reads or only writes hides the direction the task actually uses.
Do this insteadPick the most specific type that matches the task, and use stringstream only when both directions are needed.
Common mistake Assuming the variable name determines the direction
Why it failsIn the declaration patterns, s is simply the name you choose for the stream object. The direction comes from the type name.
Do this insteadRead the type name, such as istringstream or ostringstream, to find the direction, and treat s as a label.
Practice Choosing a Stream Type
For each task, name the string stream type you would choose and justify it using the direction the task needs. Task A: a program must take a sentence and pull out its numbers. Task B: a program must build a report line by line inside a string. Task C: a program must read a line of text, change some of its values, and write the updated text back into a string.
Hints
- Ask first whether the task reads from a string, then whether it writes to one.
- Task A reads from a string only, so think about which type the name signals for reading.
- Task C needs both directions, so consider which type the source describes as supporting both.
Key Takeaways
- A string stream is a stream that works with a string instead of a keyboard, a file, or the screen.
- istringstream is used for reading from a string, and its name signals input.
- ostringstream is used for writing to a string, and its name signals output.
- stringstream is used for both reading from and writing to a string, so it is the general-purpose choice when both directions are needed.
- Choose a type by identifying the direction the task needs, and justify the choice by naming that direction.