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

发布于 2024-10-19 05:39:17 字数 66 浏览 1 评论 0原文

将 std::wstring 转换为数字类型(例如 int、long、float 或 double)的最佳方法是什么?

What's the best way to convert std::wstring to numeric type, such as int, long, float or double?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

爱的故事 2024-10-26 05:39:17

C++0x 引入了以下 中的函数

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 内的转换结束(由转换函数设置)。

C++0x introduces the following functions in <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 is an optionally null pointer to the end of the conversion within str (set by the conversion function).

任性一次 2024-10-26 05:39:17

使用 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 快得多,但会增加编译时间。

Either use 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);

These will throw a boost::bad_lexical_cast exception if the string can't be converted.

The other option is to use Boost Qi (a sublibrary of 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

Using Qi is much faster than lexical_cast but will increase your compile times.

溺孤伤于心 2024-10-26 05:39:17

最好的?

如果您不想使用除 CRT 库以外的任何内容,并且在字符串无法转换时对获得 0 感到满意,那么您可以节省错误处理、复杂语法(包括标头),

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

这一切都取决于您在做什么。这就是KISS方式!

Best?

If you don't want to use anything more than the CRT library, and are happy with getting 0 if the string cannot be converted, then you can save on error handling, complex syntax, including headers by

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

It all depends what you are doing. This is the KISS way!

稀香 2024-10-26 05:39:17

使用 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;
}

Use 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;
}
椒妓 2024-10-26 05:39:17

只需使用字符串流:
不要忘记#include

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;

just use the stringstream:
do not forget to #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;
微暖i 2024-10-26 05:39:17

所以我正在使用 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;
}

So I was using Embarcadero and that piece of ..... didn´t let me use stoi, so i have to create my own function.

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文