When writing an application, we often use a string. <string> and <sstream> in the C + + standard library provide a lot of convenience for us to manipulate strings, such as Object encapsulation, security and automatic type conversion, direct stitching, no need to worry about boundaries, and so on. But today we do not want to proving to introduce the functions provided by these standard libraries, but to share an interesting phenomenon of stringstream.str (). Let's take a look at an example:
1 #include <string>
2 #include <sstream>
3 #include <iostream>
4
5 using namespace Std;
6
7 int Main ()
8 {
9 StringStream SS ("012345678901234567890123456789012345678901234567890123456789");
StringStream t_ss ("abcdefghijklmnopqrstuvwxyz");
One string str1 (Ss.str ());
12
Const char* CSTR1 = Str1.c_str ();
Const char* CSTR2 = Ss.str (). C_STR ();
Const char* CSTR3 = Ss.str (). C_STR ();
Const char* CSTR4 = Ss.str (). C_STR ();
The const char* T_CSTR = T_ss.str (). C_STR ();
18
cout << "------The results----------" << Endl
<< "Cstr1:\t" << cstr1 << Endl
<< "Cstr2:\t" << cstr2 << Endl
<< "Cstr3:\t" << cstr3 << Endl
<< "Cstr4:\t" << cstr4 << Endl
<< "T_cstr:\t" << t_cstr << Endl
<< "-----------------------------" << Endl;
26
return 0;
28}