Examples of StringStream in C + +

Source: Internet
Author: User

Contains header files #include <sstream>

Initialization can be assigned using clear (), str ();

can use << input and >> output

String S;stringstream ss;int A, B, C;getline (CIN, s); Ss.clear (); Ss.str (s); SS >> a >> b >> c; The default space here is a direct word, and even if the type of ABC is different, it is equivalent to flexible type conversion
String S;stringstream ss;int N, I, Sum, a;cin >> n;getline (CIN, s); Read line break for (i=0; i<n; i++) {    getline (CIN, s);    Ss.clear ();    SS.STR (s);    sum=0;    while (1)    {        ss >> A;        if (Ss.fail ()) break; Fail () to determine if the sum+=a is in the end        ;    }    cout << sum << Endl;}

  

    StringStream is convenient for split      . String A, B, C, D;      String lines= "ADFA;ASDFASD;FASDF;CCC";       StringStream line (lines);  Getline can be initialized directly    (line, A, ' f ');      Getline (line, B, '; ');      Getline (line, C, '; ');      Getline (line, d);     Paired with different getline    cout<< "a =" <<a<<endl;      cout<< "b =" <<b<<endl;      cout<< "c =" <<c<<endl;      cout<< "D =" <<d<<endl;  

There is another problem: the default participle here is a space, then if it is other such as comma how to do

Examples of StringStream in C + +

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.