String Streams
Topics
String Streams
- Allows you to build or extract a string
- Works similarly to other streams we’ve seen (file streams, I/O streams)
- Can use stream manipulators like file streams and I/O streams
-
setw()
, left
, right
, setfill()
, etc.
- Use
#include <sstream>
to utilize
Purpose
- Input String Stream (
istringstream
)
- Extract integers, doubles, and other data types from strings.
- Uses a string as a data source
- Output String Stream (ostringstream)
- Insert integers, doubles, and other data types into strings.
istringstream
- Best way in C++ to extract data from a string
-
string data = "10 20.5 30";
- This is a C++ string containing the following characters:
- ‘1’, ‘0’, ‘ ‘, ‘2’, ‘0’, ‘.’, ‘5’, ‘ ‘, ‘3’, ‘0’
- How do I store 10, 20.5, and 30 as an individual integer, double, and another integer?
Note: C-style strings always terminate with the null character, which is defined as backslash-zero (‘\0
’)
1
2
3
4
5
6
7
8
9
10
11
12
|
#include <sstream> // sstream is needed for istringstream and ostringstream.
#include <string>
using namespace std;
int main() {
int a;
double b;
int c;
string data = "10 20.5 30";
istringstream sin(data); // istringstream can be given a string, a C-style string, or a literal "such as this".
sin >> a >> b >> c; // istringstream has the extraction operator (>>) and can extract into different data types, JUST LIKE cin!
return 0;
}
|
1
2
3
4
5
|
istringstream sin; // istringstream does not have to take a string immediately.
string data = "10 20.5 30";
sin.str(data); // sin has a member function called .str() which means "string". For an istringstream, this PROVIDES a string to sin.
sin >> a >> b >> c;
// 10 20.5 30
|
Reusing an istringstream
- You must clear the istringstream flags prior to giving it another string.
1
2
3
4
5
6
7
|
istringstream sin("Some Data");
sin >> some_string1 >> some_string2;
sin.clear();
/* .str() will be ignored if .clear() is not called!!
* You must use .clear() FIRST and then call .str().
*/
sin.str("Some Other Data");
|
ostringstream
1
2
3
4
5
6
7
8
9
10
11
12
13
|
#include <sstream> // sstream header contains both istringstream and ostringstream.
#include <string>
using namespace std;
int main()
{
int a = 10, b = 20, c = 30;
ostringstream sout;
string output;
sout << a << ' ' << b << ' ' << c << '\n'; // Unlike istringstream, ostringstream's .str() RETURNS the string that was built.
output = sout.str(); // *Notice there are NO parameters to .str()!
cout << output;
return 0;
}
|
Reusing ostringstream
1
2
3
4
5
6
7
8
9
10
11
|
int a = 10, b = 20, c = 30;
ostringstream sout;
string output;
sout << a << ' ' << b << ' ' << c << '\n';
output = sout.str();
cout << output;
sout << 40 << ' ' << 50 << '\n'; // This will print 10 20 30 40 50
output = sout.str(); // ostringstreams keep building on the string. You can clear this by passing an empty string to .str().
cout << output;
sout.str("");
output = sout.str(); // The output string in this case will now be blank. We cleared the ostringstream by passing "" to .str().
|
stringstream
- stringstream is bi-directional. You can build strings and extract from them.
- DO NOT use stringstreams in this course!
- They are prone to errors if you mix extraction and insertion.
Use Cases
Use Cases: Argv/Argc
- When you have command line arguments that are numerical types (such as int, double, etc), argv/argc store the data as a C-style string
- In order to extract the data into the appropriate type, use an istringstream
- See Argv/Argc slides
Use Cases: Getline
- Using the getline function, we can read an entire line from a stream
- All characters up to (but not including) a ‘
\n
’ are stored into a string variable
1
|
getline(streamToReadFrom, stringToStore);
|
- If we grab lines of input from cin and wish to store parts of that input as various data types, we can take the string created from getline() and pick it apart with a string stream
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
#include <sstream> //used for string streams
#include <string> //used for getline
using namespace std;
int main() {
// read input
string input;
getline(cin, input);
// initialize string stream
istringstream sin(input);
// extract input
string name;
string course;
int grade;
sin >> name >> course >> grade;
}
|
Powerpoint