Confusion Regarding the Behavior of get() Function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <sstream>
#include <iostream>
int main()
{
    std::istringstream s1("Hello, world.");
    char c1 = s1.get(); // reads 'H'
    std::cout << "after reading " << c1 << ", gcount() == " <<  s1.gcount() << '\n';
    char c2;
    s1.get(c2);         // reads 'e'
    char str[5];
    s1.get(str, 5);     // reads "llo,"
    std::cout << "after reading " << str << ", gcount() == " <<  s1.gcount() << '\n';
    std::cout << c1 << c2 << str;
    s1.get(*std::cout.rdbuf()); // reads the rest, not including '\n'
    std::cout << "\nAfter the last get(), gcount() == " << s1.gcount() << '\n';
}


Why does the get function read the character 'e' when called in the 9th line of the code? Why not 'H'? Is the std::istringstream object s1 responsible for this action or is this due to the properties of the get() function?

Also, in the following function definition of get()

basic_istream& get( basic_streambuf& strbuf, char_type delim );

What is a streambuf object? Is it cin or something else?
Last edited on
closed account (Dy7SLyTq)
It gets the next character from the stream
@DTSCode :/ I can see that too.

I'm asking,

Is the std::istringstream object s1 responsible for this action or is this due to the properties of the get() function?


and,

What is a streambuf object? Is it cin or something else?
Again, no expert, but i would say it is from the properties of the get() function. The get() function is just reading the next character it is supposed to read in the stream. s1 is just the stream itself.

I would answer the streambuf, but I have limited knowledge at the moment. it looks like it would be like cin, but again not sure.
@audioace

I wasn't exactly sure about it but I had thought about get() being responsible for successive inputs. I just wanted to confirm it.

Still confused about this function definition though.

basic_istream& get( basic_streambuf& strbuf, char_type delim );
closed account (Dy7SLyTq)
Its a base class for builing a stream... ie its not like cin or an objrct
Topic archived. No new replies allowed.