Stringstream string stream of C/C ++ STL container

Source: Internet
Author: User

 

Header file for input and output <iostream>
Header file of the string stream <sstream>
File stream header file <fstream>

Usage of stringstream

1. Use input and output for Data Conversion
StringstreamSs_stream;
Ss_stream <I;// Input int to the stream
Ss_stream>Str;// Output the value in ss_stream to Str

// Note: if multiple data conversions are performed, you must call clear () to set the conversion mode.
Ss_stream <"456";
Ss_stream> I;// First convert the string to int
Ss_stream.Clear();
Ss_stream <True;
Ss_stream> I;// Convert the bool type to int type. If clear is not performed before, the I will fail.

// Result of running clear
I = 456.
I = 1
// No result for running clear
I = 456.
I = 8800090900.

 

 

2. Support for input and output of char *
CharSz_buf [20];
Ss_stream <8888;
Ss_stream> sz_buf; // directly output the number to the sz_buf character array.

 

3. to store the list of variable data
 Stringstream Ss_stream;
Ss_stream < "String 1" < Endl ;
Ss_stream < "String 2" < Endl ;
Ss_stream < "String 3" < Endl ;
Ss_stream <"String 4" < Endl ;
Ss_stream < "String 5" < Endl ;

Char Buffer [100];
While (Ss_stream.getline (buffer, Sizeof (Buffer ))
{
Printf ( "MSG = % s \ n" , Buffer );
}
Ss_stream ("" ); // Release resources in the string stream

// Or use string to receive
Stringstream Ss_stream;
String Stemp;
While (Getline (ss_stream, stemp ))
{
Task_download (stemp. C_str (), Relate. C_str ());
}

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.