sstream


Header: <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.

Key Characteristics

Example 1: Basic Usage: Parsing Integers from a String

#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;
}

Explanation:

Example 2: Formatting Output: Creating a CSV Line

#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;
}

Explanation:

Example 3: Type Conversion: String to Number and Vice Versa

#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;
}

Explanation:

Example 4: Mixed Data Parsing: Extracting Different Types

#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;
}

Explanation:

Additional Considerations

Summary

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.

Related

Previous Page | Course Schedule | Course Content