Clear () and STR () in stringstream ()

Source: Internet
Author: User

 

Today, when using stringstream for data conversion, I encountered a problem and found that what I got was not the expected result.

The simplified code is as follows:

 

#include <cstdlib>#include <iostream>#include <sstream> using namespace std; int main(int argc, char * argv[]){    stringstream stream;    int a,b;    stream<<"80";    stream>>a;        stream<<"90";    stream>>b;        cout<<a<<endl;    cout<<b<<endl;    system("PAUSE ");    return  EXIT_SUCCESS;} 

Running result:

The expected result of int variable B is 90, but the result of running the program is-858993460, which is obviously wrong.

After Google, the reason is that stringstream is used repeatedly because it is not cleared. See a solution: Use stringstream. Clear () to clear it before you use it again.

Test code:

#include <cstdlib>#include <iostream>#include <sstream> using namespace std; int main(int argc, char * argv[]){    stringstream stream;    int a,b;    stream<<"80";    stream>>a;        stream.clear();//        stream<<"90";    stream>>b;        cout<<a<<endl;    cout<<b<<endl;    system("PAUSE ");    return  EXIT_SUCCESS;} 

Running result:

Sure enough, we get the expected results.

But what is stringstream. Clear?

Void clear (iostate state = goodbit );
Set error state flags

Sets a new value for the error control state.

All the bits in the control state are replaced by the new ones; the value existing before the call has no effect.

If the function is called with goodbit as argument (which is the default value) All error flags are cleared.

The current State can be obtained with member function rdstate.

Clear flag space !! Check the running result of the following code:

#include <cstdlib>#include <iostream>#include <sstream> using namespace std; int main(int argc, char * argv[]){    stringstream stream;    int a,b;    stream<<"80";    stream>>a;        stream.clear();        cout<<"Size of stream = "<<stream.str().length()<<endl;    stream<<"90";    stream>>b;        cout<<"Size of stream = "<<stream.str().length()<<endl;    cout<<a<<endl;    cout<<b<<endl;    system("PAUSE ");    return  EXIT_SUCCESS;} 

Running result:

After clear (), although the result is correct, the memory occupied by stream is not released !!! Although there is no problem in our small test, in actual application, if stringstream is used multiple times, the occupied memory is increased each time, obviously there will be problems !!!

After Google continues, stringstream. STR () appears.

Void STR (const string & S );//CopiesThe content of string s toStringObject associated with the string stream buffer. the function implements tivelcils rdbuf ()-> STR (). notice that setting a new string does not clear the error flags currently set in the stream object unless the member function clear is explicitly called.

You can use stringstream. STR ("") to clear stringstream.

Test code:

#include <cstdlib>#include <iostream>#include <sstream> using namespace std; int main(int argc, char * argv[]){    stringstream stream;    int a,b;    stream<<"80";    stream>>a;    cout<<"Size of stream = "<<stream.str().length()<<endl;    stream.clear();    stream.str("");    cout<<"Size of stream = "<<stream.str().length()<<endl;    stream<<"90";    stream>>b;        cout<<"Size of stream = "<<stream.str().length()<<endl;    cout<<a<<endl;    cout<<b<<endl;    system("PAUSE ");    return  EXIT_SUCCESS;} 

 

Running result:

To sum up,

Void clear (iostate state = goodbit );//This method is not to clear the content in stringstream, but to clear the error mark of the stream!

Void STR (const string & S );//This method re-assigns a new value to stringstream.

 

So we solved the problem easily and happily.

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.