stringstream >> operator not space delimitating

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aoeuhtns
    New Member
    • Mar 2014
    • 2

    stringstream >> operator not space delimitating

    I am using a stringstream, but when I try to read out items into a string, it reads in by line not ' ' delim as I expect the behaviour to be.

    stringstream ss;
    string inputBuffer, stringBuffer;
    getline(cin, stringBuffer);
    ss << stringBuffer << '\n';
    ss >> inputBuffer;

    If stringBuffer is "This is a string", my inputBuffer gets "This is a string" not "This" as I expect.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Your target type is a string. stringstream::o perator>> will read data and format it to the target type. In this case a string.

    What deliminator are you talking about? The one for get? There is no deliminator for stringstream::O perator>>.

    Comment

    • aoeuhtns
      New Member
      • Mar 2014
      • 2

      #3
      I guess, to my understanding (which could be wrong), the >> operator is supposed to just get an item not a line. An item being until it finds a whitespace character (so, a space in this case). But when I use it, the >> acts as a getline function.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        That is correct.

        If the data is 12G 4, then a >> to an int will get 12. A following >> to a char with get G and a following >> to a short will get 4.

        However, a >> to a string will get 12G 4 because every byte of data is valid in a string.

        The reason cin >> stops on whitespace is due to the fact that istream::operat or>> does not have a overload that takes a string&. The reason scanf stops on whitespace is that none of the built-in types contain whitespace as a value (in C a string is not a type. In C++ it is).

        Comment

        Working...