Use of stringstream object

Source: Internet
Author: User

Use of stringstream object

Today, I made a case-sensitive question about String Conversion. After that, I suddenly wanted to see how others wrote it, as a result, I found a method of using the C ++ stream described in the title: it is very convenient, but even though I have completed the C ++ course, I am actually a (wu) point (li) also (to) Not (cao) knows (!) Oh, it's really embarrassing !!, It is necessary to go back and study it!

Click here
 

Use of 1stringstream object

 

# Include
 
  
# Include
  
   
Using namespace std; int main () {string line, word; while (getline (cin, line) {stringstream stream (line); cout <
   <
    
> Word) {cout <
    <
     
About
     
getline
And Related Input Usage Details can refer to the translation of another: http://blog.csdn.net/u013050857/article/details/43304025

 

Input: shanghai no1 school 1989

Output: shanghi no1 school 1989.

Shanghai

No1

School

1989

2stringstream provides conversion and formatting

 

# Include
      
       
# Includewww.bkjia.com
       
        
Using namespace std; int main () {int val1 = 512, val2 = 1024; stringstream ss; ss <
        
         
> Dump> a> dump> B; cout <
         

 

Output: val1: 512

Val2: 1024

512 1024

Part 1 in bold: reads int type into ss and changes it to string type

The second part of the black text: extract and save as int type. Of course, if a and B are declared to be of the string type, the two literal constants are saved as strings.

3 other notes

# Include
          
           
# Include
           
            
# Include
            
             
Using namespace std; int main () {stringstream ss; string s; ss <
             
              
> S; cout <
              
               

Output:

Size of stream = 19 s: shanghaisize of stream = 0

, Exercise questions HDU 2564

Code ;'

 

// General statement # include
                
                 
# Include
                 
                  
# Include
                  
                   
# Include
                   
                    
# Include using namespace std; char str [1001]; int main () {int n; int I; cin> n // enter the number of tests; getchar (); while (n --) {gets (str); // input phrase; for (I = 0; I
                    
                     
= 'A' & str [I] <= 'Z') str [I]-= 32; for (I = 0; I
                     
                       # Include
                      
                        # Include
                       
                         # Include
                        
                          # Include
                         
                           Using namespace std; char str [1001]; int main () {int n; cin> n; getchar (); while (n --) {cin. getline (str, 1000); stringstream str (str); char a [20]; while (str> a) cout <(char) toupper (a [0]); // toupper function: converts lowercase letters to uppercase letters, and tolower function-: converts uppercase letters to lowercase letters cout <
                          
                           

 

< <

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.