Difference String and StringStream

5

I'm looking at the sstream bookstore and I've run into stringstream. Googleando came to the conclusion that it is the same as string, but I can not understand it.

According to my understanding, is it to create a string and use it as a "cin" to concatenate?

#include <string>       // std::string
#include <iostream>     // std::cout
#include <sstream>      // std::stringstream

int main () {

  std::stringstream ss;

  ss << 100 << ' ' << 200;

  int foo,bar;
  ss >> foo >> bar;

  std::cout << "foo: " << foo << '\n';
  std::cout << "bar: " << bar << '\n';

  return 0;
}
    
asked by Jogofus 04.04.2017 в 16:39
source

3 answers

7

streams are exit and / or entry bufferes. Through them there is a more or less unified interface that allows you to access different data sources in a homogeneous way.

You can use an output stream to store data in a file, to write that same information on the screen or to get it out through the printer and the same is applicable for streams input.

stringstream is a stream that works on an internal buffer. It does not write in files nor read of keyboards. What you send to that stream ends up in memory. What is it for? Generally for internal uses.

The class string instead physically represents a string of characters and its interface is designed to store and manage strings of characters.

Some differences between both classes:

  • string does not have the insert / extract operator overloaded. If it is not a buffer, it does not need to behave as such.
  • string is not able to convert native types.
  • stringstream does not have iterators. A buffer is a store of information, to use it before you have to unpack it. This feature prevents it from being used directly with the vast majority of the functions of the STL.
  • stringstream does not support an explicit resizing of its internal buffer.
  • You can not directly dump the content of stringstream into another buffer. To do this you have to extract its content (for example, using the toString() method).
  • The iomanip utilities are applicable with stringstream but not with string .
answered by 04.04.2017 / 17:03
source
8

Broadly speaking, a stringstream is basically a buffer in memory that simulates behaving like a file. Simple and simple.

Allows the same operations that a stream , would allow, but is not linked to any physical device ; all operations are performed in memory, using string as warehouse .

While a stream does not allow to access the underlying physical device, a stringstream allows to access the < em> stream used as buffer , using operations

td::basic_string<CharT,Traits,Allocator> str() const;

to get a copy of the buffer , and

void str(const std::basic_string<CharT,Traits,Allocator>& new_str);

to establish us.

    
answered by 04.04.2017 в 16:52
3

The stringstream are mainly used for operations with format on string s, just like the fstream s do the same with files.

You can use all the functors that have the iostream s to manipulate the flow as the number input / output format ( std::hex , std::scientific , etc ...), strings ( std::quoted ) , fillings (std :: setw, std::setfill ), location settings ( .imbue() , std::put_money ), etc.

In other words, if iostream with its overloads of operator<</>> s is the alternative C ++ of printf , fstream is fprintf , and stringstream is sprintf , in addition to type safe and with much more comfortable syntax, as well as iterable ( std::istream_iterator and std::ostream_iterator , in order to use flows as if they were containers).

    
answered by 04.04.2017 в 17:19