STRINGSTREAM.STR () Trap for string usage

Source: Internet
Author: User

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}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.