欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C/C++數(shù)字與字符串互相轉(zhuǎn)換的實現(xiàn)示例

 更新時間:2025年02月16日 11:02:36   作者:啃雞翅的小白貓  
本文主要介紹了C/C++數(shù)字與字符串互相轉(zhuǎn)換的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

一、數(shù)字轉(zhuǎn)字符串

1.方法一(利用<sstream>的stringstream,可以是浮點數(shù)

#include <iostream>
#include <sstream>
using namespace std;
int main()
{
    double x;
    string str;
    stringstream ss;
    cin >> x;
    ss << x;
    ss >> str;
    cout << str;
    return 0;
}

2.方法二(利用<sstream>中的to_string()方法,浮點數(shù)會附帶小數(shù)點后六位,不足補零,不推薦浮點數(shù)使用

#include <iostream>
#include <sstream>
using namespace std;
int main()
{
    double x;
    string str;
    cin >> x;
    str = to_string(x);
    cout << str;
    return 0;
}

一、字符串轉(zhuǎn)數(shù)字

1.方法一(利用<sstream>的stringstream,可以是浮點數(shù))

#include <iostream>
#include <sstream>
using namespace std;
int main()
{
    double x;
    string str;
    stringstream ss;
    cin >> str;
    ss << str;
    ss >> x;
    cout << x;
    return 0;
}

2.方法二(利用<string>中的stoi()函數(shù),其中還有對于其他類型的函數(shù),如stod(),stof()等,根據(jù)類型選取

#include <iostream>
#include <string>
using namespace std;
int main()
{
    int x;
    string str;
    cin >> str;
    x = stoi(str);
    cout << x;
    return 0;
}

到此這篇關(guān)于C/C++數(shù)字與字符串互相轉(zhuǎn)換的實現(xiàn)示例的文章就介紹到這了,更多相關(guān)C/C++數(shù)字與字符串互相轉(zhuǎn)換內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家! 

相關(guān)文章

最新評論