Integer and String interconversion

Wei always confused about how to get converted, trained, trained, trained, wrote in the blog today

Integer to String
stringstream

#include <iostream>
#include <sstream>
using namespace std;
int main()
{
    int a;
    while(cin>>a)
    {
        stringstream ss;
        ss<<a;
        string s=ss.str();
        cout<<s<<endl;
    }
}

String turn integer
stringstrream

#include <iostream>
#include <sstream>
using namespace std;
int main()
{
    string a;
    while(cin>>a)
    {
        stringstream ss;
        ss<<a;
        int ans;
        ss>>ans;
        cout<<ans<<endl;
    }
}

Output: (two are this)
The result is output

Published 32 original articles · won praise 12 · views 1390

Guess you like

Origin blog.csdn.net/qq_18873031/article/details/98884352