stringstream how to use and read documentation

Hi all,
as introduced before, I am a beginner about C++. I'm studing this tutorial http://www.cplusplus.com/doc/tutorial/structures/
and I don't understand really what is it stringstream

I searched on Documentation, I found this, http://www.cplusplus.com/reference/sstream/stringstream/?kw=stringstream , but I don't actually understand how to read documentation. Could someone help me to understand to read documentation? For example what are flow chart at the beginning of the page?

In the tutorial, at the paragraph 'Pointers to structures', is stingstream is used for a cast? What does it mean?

In the previous paragraph I read this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// example about structures
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

struct movies_t {
  string title;
  int year;
} mine, yours;

void printmovie (movies_t movie);

int main ()
{
  string mystr;

  mine.title = "2001 A Space Odyssey";
  mine.year = 1968;

  cout << "Enter title: ";
  getline (cin,yours.title);
  cout << "Enter year: ";
  getline (cin,mystr);
  stringstream(mystr) >> yours.year;

  cout << "My favorite movie is:\n ";
  printmovie (mine);
  cout << "And yours is:\n ";
  printmovie (yours);
  return 0;
}

void printmovie (movies_t movie)
{
  cout << movie.title;
  cout << " (" << movie.year << ")\n";
}


Now stringstream is a function. Where is its definition? How could I find it in Documentation?


Sorry for my stupid questions, but I find a lot of difficulties to read Documentation probably formatted for experts. If someone provide to explain how to read Documentation, for general pourpuse, he will give me and others beginners a big help!

Thanks a lot!!
Last edited on
std::stringstreams are a lot like std::cout and std::cin, except they work with std::strings instead of the standard input/output.

So just like how
cout << "Hi there!";
prints "Hi there!" to the screen, you can do
1
2
3
ostringstream oss;
oss << "Hi there!" << 55555; // "Print" 'Hi there!' and 55555 to a string
string str = oss.str(); // str now contains the string "Hi there!55555" 

to "print" to a string.

Similarly, just like how you can do
1
2
3
int num;
cin >> num; // Let's say you enter "15"
// Now 'num' equals 15 

to get input from the user, you can get input from a string with stringstreams:
1
2
3
4
5
string str = "15";
int num;
istringstream iss(str);
iss >> num;
// 'num' equals 15 now 
.

So, as you can see, stringstreams are useful for converting strings to numbers and vice-versa. (They also have many other uses, but this one might be one of the more common ones.)
All right, but how to read Documentation?
Well, some of it is technical stuff that probably won't be of any interest to you if you're just starting out.
Even so, if you just read the first few paragraphs (http://www.cplusplus.com/reference/sstream/stringstream/ ), you can get the basic gist of it:
Stream class to operate on strings.

Objects of this class use a string buffer that contains a sequence of characters. This sequence of characters can be accessed directly as a string object, using member str.

Characters can be inserted and/or extracted from the stream using any operation allowed on both input and output streams.
Topic archived. No new replies allowed.