Simple Stringstream
Aug 16, 2012 at 2:01am UTC
Quick question:
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: " ;
cin >> yours.title;
cout << "Enter year: " ;
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" ;
}
This is another example from the tutorial on this website (
very slightly manipulated). Basically, I want to know if IN GENERAL (not just in this situation), "stringstream" -- on line 25 (the bold/underlined line) -- is used to change strings to data structures.
***(YES/NO), if no then what else***
Aug 16, 2012 at 7:29am UTC
Stringstream converts string into stream.Then it can be used like we use cin, cout or file objects.
Its generally used to extract other type(int generall) from string.stringstream(mystr) >> yours.year; gets years of int type from myster.
Aug 16, 2012 at 7:35am UTC
IN GENERAL (not just in this situation), "stringstream" -- on line 25 (the bold/underlined line) -- is used to change strings to data structures.
Certainly not. It is slow and supports only limited format options.
Aug 16, 2012 at 12:43pm UTC
Okay got it. Thanks.
~Solved~
Topic archived. No new replies allowed.