<sstream>
The <sstream>
header in C++ provides string stream classes that allow string-based stream operations. It's part of the Standard Template Library (STL) and offers a convenient way to perform input/output operations on strings, similar to how we work with files or console I/O.
istringstream
, ostringstream
, and stringstream
<istream>
, <ostream>
, and <iostream>
#include <iostream>
#include <sstream>
#include <vector>
int main() {
std::string input = "10 20 30 40 50";
std::istringstream iss(input);
std::vector<int> numbers;
int num;
while (iss >> num) {
numbers.push_back(num);
}
std::cout << "Parsed numbers: ";
for (int n : numbers) {
std::cout << n << " ";
}
std::cout << std::endl;
return 0;
}
istringstream
object from a string containing space-separated numbers.>>
to parse integers from the stream.#include <iostream>
#include <sstream>
#include <vector>
int main() {
std::vector<std::string> data = {"John", "Doe", "30", "New York"};
std::ostringstream oss;
for (size_t i = 0; i < data.size(); ++i) {
oss << data[i];
if (i < data.size() - 1) {
oss << ",";
}
}
std::string csv_line = oss.str();
std::cout << "CSV line: " << csv_line << std::endl;
return 0;
}
ostringstream
to build a CSV (Comma-Separated Values) line from a vector of strings.<<
operator is used to insert data into the string stream.str()
method is called to get the resulting string from the stream.#include <iostream>
#include <sstream>
#include <iomanip>
int main() {
// String to number conversion
std::string str_num = "123.456";
std::istringstream iss(str_num);
double number;
iss >> number;
std::cout << "Converted string to number: " << number << std::endl;
// Number to string conversion with formatting
double pi = 3.14159265359;
std::ostringstream oss;
oss << std::fixed << std::setprecision(2) << pi;
std::string str_pi = oss.str();
std::cout << "Converted number to string with 2 decimal places: " << str_pi << std::endl;
return 0;
}
istringstream
to convert a string representation of a number to an actual number (double).ostringstream
to convert a number to a string.std::fixed
and std::setprecision
to format the output.str()
method is used to retrieve the formatted string from the output string stream.#include <iostream>
#include <sstream>
int main() {
std::string input = "John 25 180.5";
std::istringstream iss(input);
std::string name;
int age;
double height;
iss >> name >> age >> height;
std::cout << "Name: " << name << std::endl;
std::cout << "Age: " << age << std::endl;
std::cout << "Height: " << height << " cm" << std::endl;
return 0;
}
istringstream
to parse a string containing mixed data types.>>
automatically handles type conversion for different variables.sstream
can be used to easily parse structured data from a string.sstream
, it's important to check for parsing errors, especially when dealing with user input or external data.sstream
is more useful for complex formatting or parsing tasks.stringstream
objects can be reused by calling the clear()
and str("")
methods, which can be more efficient than creating new objects for each operation.The <sstream>
header in C++ provides powerful tools for string-based stream operations. It includes istringstream
for input operations, ostringstream
for output operations, and stringstream
for both input and output. These classes are particularly useful for parsing strings, formatting output, and performing type conversions between strings and other data types.
Key applications of <sstream>
include:
1. Parsing structured data from strings
2. Formatting data into strings with precise control
3. Performing type conversions between strings and numeric types
4. Building complex strings from various data types
By leveraging the familiar stream interface, <sstream>
allows developers to apply their knowledge of I/O operations to string manipulation tasks, making it a versatile and powerful tool in C++ programming.