Input from an istringstream - C++ STL

C++ examples for STL:string

Description

Input from an istringstream

Demo Code

#include <iostream>
#include <sstream>
#include <string>

int main(int argc, const char* argv[]) {
    std::string input("Input test 123 4.7 A");
    std::istringstream inputString(input);
    std::string string1;/*ww  w.j av  a2  s. com*/
    std::string string2;
    int integer;
    double double1;
    char character;

    inputString >> string1 >> string2 >> integer >> double1 >> character;

    std::cout << "The following items were extracted\n"
              << "from the istringstream object:"
              << "\nstring: " << string1 << "\nstring: " << string2
              << "\n     int: " << integer << "\ndouble: " << double1
              << "\n    char: " << character;

    // attempt to read from empty stream
    long value;
    inputString >> value;

    // test stream results
    if (inputString.good())
        std::cout << "\n\nlong value is: " << value << std::endl;
    else
        std::cout << "\n\ninputString is empty" << std::endl;

    return 0;
}

Result


Related Tutorials