string和int類型互相轉換的C++方法

#include <iostream>
#include <string>
#include <sstream>

 

using namespace std;

 

int main()
{
    int i1 = 100;
    int i2;

    string str1 = "111000";
    string str2;

    stringstream tmp;

 

    //數字轉文字
    tmp << i1;
    tmp >> str2;
    cout << "str2= " << str2 << endl;

 

    tmp.clear(); //清空流

 

    //文字轉數字
    tmp << str1;
    tmp >> i2;
    cout << "i2= " << i2 << endl;

    system("PAUSE");

 

    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章