如何将std::wstring转换为数字类型(int、long、float)?

6

如何将 std::wstring 转换为数值类型,例如int、long、float或double,哪种方式最好?


可能是重复的问题:如何将C++字符串转换为整数? - Martin York
6个回答

36

C++0x 在 <string> 中引入了以下函数

int                stoi  (const wstring& str, size_t* idx = 0, int base = 10);
long               stol  (const wstring& str, size_t* idx = 0, int base = 10);
unsigned long      stoul (const wstring& str, size_t* idx = 0, int base = 10);
long long          stoll (const wstring& str, size_t* idx = 0, int base = 10);
unsigned long long stoull(const wstring& str, size_t* idx = 0, int base = 10);

float       stof (const wstring& str, size_t* idx = 0);
double      stod (const wstring& str, size_t* idx = 0);
long double stold(const wstring& str, size_t* idx = 0);

idx 是一个可选的指向 str 中转换结束位置的空指针(由转换函数设置)。


18

可以使用boost::lexical_cast<>

#include <boost/lexical_cast.hpp>

std::wstring s1(L"123");
int num = boost::lexical_cast<int>(s1);

std::wstring s2(L"123.5");
double d = boost::lexical_cast<double>(s2);

如果字符串无法转换,这些将引发boost::bad_lexical_cast异常。

另一个选项是使用Boost Qi(Boost.Spirit的一个子库):

#include <boost/spirit/include/qi.hpp>

std::wstring s1(L"123");
int num = 0;
if (boost::spirit::qi::parse(s1.begin(), s1.end(), num))
    ; // conversion successful

std::wstring s2(L"123.5");
double d = 0;
if (boost::spirit::qi::parse(s1.begin(), s1.end(), d))
    ; // conversion successful

使用 Qi 比 lexical_cast 快得多,但会增加编译时间。


4
人们声称C++过于复杂! - Martin Beckett

13

最佳方案?

如果您不想使用除CRT库之外的任何内容,并且即使字符串无法转换也满足得到0的要求,那么可以通过以下方式节省错误处理、复杂语法和包括头文件:

std::wstring s(L"123.5");
float value = (float) _wtof( s.c_str() );

这完全取决于你在做什么。这就是“保持简单”的方式!


2
_wtof 是从哪里来的? - David Thornley
@david C运行时库(CRT) - ravenspoint
1
我在C99标准或Harbison&Steele中也没有看到它的参考。也许wcstod / wcstol / wcstoul会更好。 - David Thornley

3
使用wstringstream / stringstream
#include <sstream>
float toFloat(const std::wstring& strbuf)
{
    std::wstringstream converter;
    float value = 0;

    converter.precision(4);
    converter.fill('0');
    converter.setf( std::ios::fixed, std::ios::floatfield );                              

    converter << strbuf;
    converter >> value;
    return value;
}

1
只需使用stringstream: 不要忘记#include <sstream>
wchar_t blank;
wstring sInt(L"123");
wstring sFloat(L"123.456");
wstring sLong(L"1234567890");
int rInt;
float rFloat;
long rLong;

wstringstream convStream;

convStream << sInt<<' '<< sFloat<<' '<<sLong;
convStream >> rInt;
convStream >> rFloat;
convStream >> rLong;
cout << rInt << endl << rFloat << endl << rLong << endl;

-1

所以我正在使用 Embarcadero,但那个东西不让我使用 stoi,所以我不得不创建自己的函数。

int string_int(wstring lala){
    int numero;
    int completo = 0;
    int exponente = 1;
    int l = 1;
    for (int i = 0; i < lala.size(); i++){
        numero = 0;
        for (int j = 48; j < 57; j++){
            if (lala[i] == j){
                break;
            }
            numero++;
        }
        for (int k = 0; k < lala.size() - l; k++){
            exponente *= 10;
        }
        numero *= exponente;
        completo += numero;
        exponente = 1;
        l++;
    }
    return completo;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接