Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

The C++ standard library

The C++ standard library

In C++ programming, handling conversions between strings and numbers is a common task.

sstream is a component in the C++ standard library that provides a convenient way to handle string streams (strings can be handled like streams).

allows you to use strings as input/output streams, which makes reading data from strings or writing data to strings very simple.

define

sstreamis a namespace in the C++ standard library that contains several classes for handling string streams, including:

Syntax

UsesstreamThe basic syntax is as follows:

#include 



// Using istringstream

std::istringstream iss("some data");



// Using ostringstream

std::ostringstream oss;



// Using stringstream

std::stringstream ss;

Example

Reading data from a string

Below is an example using istringstream An example of reading integers and floating-point numbers from a string:

Example

#include

#include

int

main

(

)

{

    std

::

string

data

=

"10 20.5"

;

    std

::

istringstream

iss

(

data

)

;

   

int

i

;

   

double

d

;

    iss

i

d

;

    std

::

cout

<<

"Integer: "

<<

i

<<

std

::

endl

;

    std

::

cout

<<

"Double: "

<<

d

<<

std

::

endl

;

   

return

0

;

}

Output result:

Integer: 10

Double: 20.5

Writing data to a string

Below is an example using ostringstream An example of writing data to a string:

Example

#include

#include

int

main

(

)

{

    std

::

ostringstream

oss

;

   

int

i

=

100

;

   

double

d

=

200.5

;

    oss

<<

i

<<

" "

<<

d

;

    std

::

string

result

=

oss.

str

(

)

;

    std

::

cout

<<

"Resulting string: "

<<

result

<<

std

::

endl

;

   

return

0

;

}

Output result:

Resulting string: 100 200.5

Using stringstream for read and write operations

Below is an example using stringstream An example of simultaneous read and write operations:

Example

#include

#include

int

main

(

)

{

    std

::

string

data

=

"30 40.5"

;

    std

::

stringstream

ss

(

data

)

;

   

int

i

;

   

double

d

;

   

// Read data from stringstream

    ss

i

d

;

    std

::

cout

<<

"Read Integer: "

<<

i

<<

", Double: "

<<

d

<<

std

::

endl

;

   

// Write data to stringstream

    ss.

str

(

""

)

;

// Clear stringstream

    ss

<<

"New data: "

<<

50

<<

" "

<<

60.7

;

    std

::

string

newData

=

ss.

str

(

)

;

    std

::

cout

<<

"New data string: "

<<

newData

<<

std

::

endl

;

   

return

0

;

}

Output result:

Read Integer: 30, Double: 40.5

New data string: New data: 50 60.7

Summary

sstream is a very useful component in the C++ standard library; it simplifies conversions between strings and basic data types. Through the above examples, we can see how to use istringstream, ostringstream and stringstream to perform these conversions. Mastering these skills will help you handle string data more efficiently in C++ programming.