Stringstream usage Summary

Source: Internet
Author: User

Today, I looked at C ++ cookbook and saw a very useful thing, stringstream, which can easily complete connection string, hexadecimal conversion, format control, and other work. It is located in sstream. h.

 
# Include <iostream>

# Include <iomanip>

 
# Include <string>

# Include <sstream>

 
 

Using NamespaceSTD;

 
 

IntMain ()

 
{

Stringstream SS;

SS <"There are"<9 <"Apples in my cart .";

Cout <ss. STR () <Endl;

 
 

SS. STR ("");

 
SS <showbase  

Cout <ss. STR () <Endl;

 
 

SS. STR ("");

 
SS <setprecision (4) <3.1415927;

Cout <ss. STR () <Endl;

 
 

Cin> SS. STR ();

 
}

Use initialization:

Stringstream. STR ("..")

The other methods are almost the same as the input and output streams. The control words in iomanip can be used to output different formats. The corresponding strings can be obtained using ss. STR.

For example

 
SS <showbase  

 

Indicates

1) Output hexadecimal notation (0x here)

2) Output hex)

Another example:

DoubleScitodub (ConstString & Str)

 
{

Stringstream SS (STR );

 
DoubleD = 0;

SS> D;

 
 

If(Ss. Fail ())

 
{

// Add code to solve the exception

 
}

 

 
ReturnD;

}

The stringstream> double method can be called to convert the string represented by the scientific notation to the double type, and check whether the conversion is successful by stringstream. Fail ().

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.