Recently I have met a small tricky problem about parsing and formatting the string input “yyyy/mm/dd” in C++ programming question, and the whole solution procedure is here. To nicely solve it, I used std::stringstream
, which belongs to the <sstream>
library. I used this tool to manipulate string a long time ago, but now I want to write a mini-blog to summarize it.
What is std::stringstream
?
std::stringstream
is a part of the Standard Library’s I/O facilities in C++ that lets you operate on strings in a way similar to how cin
or cout
work with input and output. It can be used to perform operations like extracting data from strings, formatting strings, and converting between different types. Essentially, std::stringstream
treats strings like an input/output stream, making it very versatile for handling formatted data.
Basic Syntax
To use std::stringstream
, include the <sstream>
library:
#include <sstream>
Here are the basic steps:
- Create an instance of
std::stringstream
. - Load a string into the stream.
- Extract values from the stream or insert values into it.
Example Usage: Parsing a Date
Here I will take the C++ programming question mentioned above as an example.
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;
bool isLeapYear(int year) {
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
int main() {
string input;
getline(cin, input);
int year, month, day;
char delimiter;
stringstream ss(input);
ss >> year >> delimiter >> month >> delimiter >> day;
vector<int> daysInMonth = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (isLeapYear(year)) {
daysInMonth[1] = 29;
}
int dayOfYear = 0;
for (int i = 0; i < month - 1; ++i) {
dayOfYear += daysInMonth[i];
}
dayOfYear += day;
cout << dayOfYear << endl;
system("pause");
return 0;
}
Code Explanation
- To use it, you should include the necessary library
#include <sstream>
first. - Use
getline(cin, input)
reads the entire date string from the user. stringstream ss(input)
initializes the stream with the input string.ss >> year >> delimiter >> month >> delimiter >> day
extracts the values intoyear
,month
, andday
. The delimiter variable holds the ‘/’ characters in between, effectively skipping them.
Using Benefits
- Handling Delimiters: When extracting values,
std::stringstream
automatically skips spaces or specified delimiters. - Conversion Between Data Types: It easily handles type conversion—for instance, converting a
string
representation of an integer to anint
type. - Error Handling: If the format of the input string is not correct,
std::stringstream
gracefully handles errors by setting the error state flags, making it more robust than manual parsing.
Inclusion
std::stringstream
is an invaluable tool for any C++ programmer who needs to work with strings that contain formatted data. Whether you need to parse user input, convert between data types, or format strings for output, std::stringstream
makes these operations simple and intuitive.