stringstream

Feb 10, 2014 at 4:39pm
Hello World. I want to ask how to get the full string from ss to b. And what make exactly stringstream? Is it something like a pointer? Can I delete it when I have finished working with it?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <sstream>

using namespace std;

int main () {
    char a [12]="Hello World";
    stringstream ss;
    string b;
    ss << a;
    ss >> b;
    cout << b << endl;
    cout << a << endl;
    return 0;
}
Feb 10, 2014 at 4:45pm
stringstream, just like most c++ objects have their own internal implementations that enable them to allocate and deallocate memory as needed. Thus eliminating the need for the user to handle this themselves.

To get the full string from ss to b, you can make use of the str method for stringstream objects or use getline seeing as stringstream inherits from istream class:

1
2
3
4
5
getline(ss, b);

// or

b = ss.str();


http://www.cplusplus.com/reference/sstream/stringstream/str/
http://www.cplusplus.com/reference/string/string/getline/
Feb 10, 2014 at 4:53pm
But if we have vice versa?String to char.
Feb 10, 2014 at 5:21pm
Can you expand on that? What do you mean by vice versa?
Feb 10, 2014 at 5:23pm
I want to ask how to get the full string from ss to b.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <sstream>

using namespace std;

int main () {
    string a ="Hello World";
    stringstream ss;
    char b [12];
    ss << a;
    ss >> b;
    cout << b << endl;
    cout << a << endl;
    return 0;
}

Feb 10, 2014 at 5:25pm
Is it possible?
Feb 10, 2014 at 5:32pm
Yes, if you follow Smac89's advice. You need to use the str() method to retrieve everthing in the stringstream's buffer.

If you mean each character from a stringstream, you could try a loop and insert a character at a time into a character variable, printing out its value within the loop.
Feb 10, 2014 at 5:32pm
Please, take me the code.
Feb 10, 2014 at 5:33pm
You can use getline.

 
ss.getline(b, 12);
Feb 10, 2014 at 5:35pm
Thank you.
Topic archived. No new replies allowed.