0

hi I've read several articles about the streams and i know a bit about them . for example they are not random access they are serial interface or for example they are a flow of data ..as far as I'am concerned so but what are they really ..this what is making me really confused and i don't get the point of it you know my main problem with the concept is that i don't understand the abstraction let me give you an example :

strign S;
std::cin>>string S; 

so as far as i konw when we write this this means that the "cin" is a iostream object that the ">>" is a overloaded operation for iostream objects and cin is synced to keyboard .so it reads the data from keyboard and then the string "S" is some how a distention for cin stream and then the data which is captured from the keyboard are put to the string "S" or let's consider this code :

    string largest_number(vector<string> a) {

         std::stringstream ret;
         for (size_t i = 0; i < a.size(); i++) {
                ret << a[i];
        } 
        string result;
        ret >> result;
       return result;
      }

in this code what i understand is that the ""ret"" is a stringstream as it is obvious by declaration and does it only read the string type input??(i guess so) and then by the operator ""<<"" we put the data to the stream and then by "">>"" we some how put the data in ""result"" and because the ""ret"" is a stream the result could be any type which is chosen string here i know this much but still i feel that I'm far away from understanding the core concept of streams if any one could help me with this concept and give's me a very gentle idea to get the gist of the streams

  • `std::cin>>string S` doesn't compile. – Fred Larson Apr 24 '20 at 12:55
  • There is an overloaded operator `std::stream& operator>>(std::stream &in, std::string &str)` (together with similar for other types). It reads characters from `in` and stores them into `str` until certain conditions are met (e.g. a white space is found or end of file is reached or something else). Thereby `in` provides states which have to be considered. However, afterwards, `in` is returned (`return in;`) to allow chaining of such input operators. The basic idea was to provide a type save, simple to use, and intuitive alternative for formatted input to replace the C `scanf()` function family. – Scheff's Cat Apr 24 '20 at 13:10
  • `std::stringstream` is a stream which reads from a string (instead of e.g. a file). It can read _into_ all the same types like `std::fstream` or `std::cin`. Imagine that a contents may be either read from file or from memory (which has been filled e.g. by network I/O before). Isn't it nice that you can provide a read function with a `std::stream&` parameter which will work independently of whether it's an `fstream` or `stringstream` or `cin` it has to read from? – Scheff's Cat Apr 24 '20 at 13:14

0 Answers0