C++ 如何将 std::wstring 转换为数字类型(int、long、float)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5118308/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert std::wstring to numeric type(int, long, float)?
提问by Hyden
What's the best way to convert std::wstring to numeric type, such as int, long, float or double?
将 std::wstring 转换为数字类型(例如 int、long、float 或 double)的最佳方法是什么?
回答by Howard Hinnant
C++0x introduces the followingfunctionsin <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).
idx
是一个可选的空指针,指向其中的转换结束str
(由转换函数设置)。
回答by hkaiser
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.
boost::bad_lexical_cast
如果字符串无法转换,这些将抛出异常。
The other option is to use Boost Qi (a sublibrary of Boost.Spirit):
另一种选择是使用 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
Using Qi is much faster than lexical_cast but will increase your compile times.
使用 Qi 比 lexical_cast 快得多,但会增加编译时间。
回答by ravenspoint
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
如果您不想使用 CRT 库以外的任何东西,并且在无法转换字符串时获得 0 感到满意,那么您可以节省错误处理、复杂的语法,包括头文件
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!
这一切都取决于你在做什么。这就是KISS的方式!
回答by Julio Gorgé
Use wstringstream / stringstream:
使用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;
}
回答by BlueWhaleMobile
just use the stringstream:
do not forget to #include <sstream>
只需使用字符串流:不要忘记 #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;
回答by Eduardo Alonzo
So I was using Embarcadero and that piece of ..... didn′t let me use stoi, so i have to create my own function.
所以我在使用 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;
}