A better example?

Hi,

First of all, this is a not-so-important suggestion to the admins, so if it is in the wrong section, please point me in the right place.

I was reading about stringstream, which inevitably lead me to stringbuf, which, again, inevitably lead me to str() member function. And on that page(http://www.cplusplus.com/reference/iostream/stringbuf/str/), there is an example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// stringbuf::str
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main () {

  stringbuf sb;
  string mystr;

  sb.sputn ("Sample string",13);
  mystr=sb.str();

  cout << mystr;

  return 0;
}


I simply thought that readers will find the function and usage of str() more clear if instead of
sb.sputn ("Sample string",13);(line 13)
should be used
sb.str(sampleString);.
Of course, for that to have any sense, there will be a need for another line which will assign "Sample string"(or any other string) to the string object sampleString.
And thats all! :D.

So this is how it should look like. The changed parts are underlined:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// stringbuf::str
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main () {

  stringbuf sb;
  string mystr,sampleString;

  sampleString="Sample String");
  sb.str (sampleString);
  mystr=sb.str();

  cout << mystr;

  return 0;
}


I wrote this only because that partucalr section is explaining the usage of str(), and yet it doesn't uses it in its own example. I find that silly :]

Thanks for reading all this for such a minor thing! Stay with health(RL)!
Topic archived. No new replies allowed.