stringstream

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;
}
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/
But if we have vice versa?String to char.
Can you expand on that? What do you mean by vice versa?
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;
}

Is it possible?
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.
Please, take me the code.
You can use getline.

 
ss.getline(b, 12);
Thank you.
Topic archived. No new replies allowed.