020 string與數(shù)值轉(zhuǎn)換

C++ 11 引入了多個函數(shù),可以實現(xiàn)數(shù)值數(shù)據(jù)與標(biāo)準(zhǔn)庫 string 之間的轉(zhuǎn)換。

數(shù)值類型 轉(zhuǎn)換為 string

to_string(val) 定義在 頭文件 <string> 中,它是一組重載函數(shù),返回值 val 的 string 表示。

    std::string to_string( int value );
    std::string to_string( long value );
    std::string to_string( long long value );
    std::string to_string( unsigned value );
    std::string to_string( unsigned long value );
    std::string to_string( unsigned long long value );
    std::string to_string( float value );
    std::string to_string( double value );
    std::string to_string( long double value );

注意:

  1. 使用 std::to_string 轉(zhuǎn)換浮點類型可能會產(chǎn)生意想不到的結(jié)果,因為返回的字符串中的有效數(shù)字可能為零,請參見下面的示例。
  2. 返回值可能與默認(rèn)情況下 std::cout 打印的值有很大的不同,請參見下面的示例。
  3. std::to_string 依賴于當(dāng)前語言環(huán)境進(jìn)行格式化,因此來自多個線程的對 std::to_string 的并發(fā)調(diào)用可能導(dǎo)致調(diào)用的部分序列化。為此,c++ 17 提供 std::to_chars 作為一種與語言環(huán)境無關(guān)的高性能替代方案。

測試示例:

#include <QCoreApplication>
#include <iostream>
#include <string>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    double f = 23.43;
    double f2 = 1e-9;
    double f3 = 1e40;
    double f4 = 1e-40;
    double f5 = 123456789;
    std::string f_str = std::to_string(f);
    std::string f_str2 = std::to_string(f2); // Note: returns "0.000000"
    std::string f_str3 = std::to_string(f3); // Note: Does not return "1e+40".
    std::string f_str4 = std::to_string(f4); // Note: returns "0.000000"
    std::string f_str5 = std::to_string(f5);
    std::cout << "std::cout: " << f << '\n'
              << "to_string: " << f_str  << "\n\n"
              << "std::cout: " << f2 << '\n'
              << "to_string: " << f_str2 << "\n\n"
              << "std::cout: " << f3 << '\n'
              << "to_string: " << f_str3 << "\n\n"
              << "std::cout: " << f4 << '\n'
              << "to_string: " << f_str4 << "\n\n"
              << "std::cout: " << f5 << '\n'
              << "to_string: " << f_str5 << '\n';

    return a.exec();
}

輸出結(jié)果:

std::cout: 23.43
to_string: 23.430000

std::cout: 1e-09
to_string: 0.000000

std::cout: 1e+40
to_string: 10000000000000000303786028427003666890752.000000

std::cout: 1e-40
to_string: 0.000000

std::cout: 1.23457e+08
to_string: 123456789.000000

string 轉(zhuǎn)換為 數(shù)值類型

    int stoi( const std::string& str, std::size_t* pos = nullptr, int base = 10 );
    int stoi( const std::wstring& str, std::size_t* pos = nullptr, int base = 10 );
    long stol( const std::string& str, std::size_t* pos = nullptr, int base = 10 );
    long stol( const std::wstring& str, std::size_t* pos = nullptr, int base = 10 );
    long long stoll( const std::string& str, std::size_t* pos = nullptr, int base = 10 );
    long long stoll( const std::wstring& str, std::size_t* pos = nullptr, int base = 10 );
    unsigned long stoul( const std::string& str, std::size_t* pos = nullptr, int base = 10 );
    unsigned long stoul( const std::wstring& str, std::size_t* pos = nullptr, int base = 10 );
    unsigned long long stoull( const std::string& str, std::size_t* pos = nullptr, int base = 10 );
    unsigned long long stoull( const std::wstring& str, std::size_t* pos = nullptr, int base = 10 );

    float stof( const std::string& str, std::size_t* pos = nullptr );
    float stof( const std::wstring& str, std::size_t* pos = nullptr );
    double stod( const std::string& str, std::size_t* pos = nullptr );
    double stod( const std::wstring& str, std::size_t* pos = nullptr );
    long double stold( const std::string& str, std::size_t* pos = nullptr );
    long double stold( const std::wstring& str, std::size_t* pos = nullptr );

使用示例:

#include <QCoreApplication>
#include <iostream>
#include <string>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    std::string str1 = "45";
    std::string str2 = "3.14159";
    std::string str3 = "31337 with words";
    std::string str4 = "words and 2";

    int myint1 = std::stoi(str1);
    int myint2 = std::stoi(str2);
    int myint3 = std::stoi(str3);
    // error: 'std::invalid_argument'
    // int myint4 = std::stoi(str4);

    std::cout << "std::stoi(\"" << str1 << "\") is " << myint1 << '\n';
    std::cout << "std::stoi(\"" << str2 << "\") is " << myint2 << '\n';
    std::cout << "std::stoi(\"" << str3 << "\") is " << myint3 << '\n';
    //std::cout << "std::stoi(\"" << str4 << "\") is " << myint4 << '\n';

    return a.exec();
}

輸出結(jié)果:

std::stoi("45") is 45
std::stoi("3.14159") is 3
std::stoi("31337 with words") is 31337
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容