std :: lexical_cast - 有这样的事吗? [英] std::lexical_cast - is there such a thing?

查看:719
本文介绍了std :: lexical_cast - 有这样的事吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++标准库定义了这个函数,还是我必须求助于Boost?



我在网络上搜索过,除了Boost,












<对于内置类型,p> C ++ 11 < string> std :: to_string p>


[n3290:21.5 / 7]:

  string to_string(int val); 
string to_string(unsigned val);
string to_string(long val);
string to_string(unsigned long val);
string to_string(long long val);
string to_string(unsigned long long val);
string to_string(float val);
string to_string(double val);
string to_string(long double val);

返回:每个函数返回 string 对象保存其参数值的
字符表示,
通过调用 sprintf(buf,fmt,val)生成格式
%d%u%ld%lu%lld%llu
%f%f 或%Lf,其中 buf


还有以下情况:


[n3290:21.5 / 1,21.5 / 4]:

  int stoi(con​​st string& str,size_t * idx = 0,int base = 10); 
long stol(const string& str,size_t * idx = 0,int base = 10);
unsigned long stoul(const string& str,size_t * idx = 0,int base = 10);
long long stoll(const string& str,size_t * idx = 0,int base = 10);
unsigned long long stoull(const string& str,size_t * idx = 0,int base = 10);
float stof(const string& str,size_t * idx = 0);
double stod(const string& str,size_t * idx = 0);
long double stold(const string& str,size_t * idx = 0);


但是,没有什么可以使用的通用a href =http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n1973.html>直到TR2 ,也许!),什么都没有C ++ 03。


Does the C++ Standard Library define this function, or do I have to resort to Boost?

I searched the web and couldn't find anything except Boost, but I thought I'd better ask here.

解决方案

Only partially.

C++11 <string> has std::to_string for the built-in types:

[n3290: 21.5/7]:

string to_string(int val);
string to_string(unsigned val);
string to_string(long val);
string to_string(unsigned long val);
string to_string(long long val);
string to_string(unsigned long long val);
string to_string(float val);
string to_string(double val);
string to_string(long double val);

Returns: Each function returns a string object holding the character representation of the value of its argument that would be generated by calling sprintf(buf, fmt, val) with a format specifier of "%d", "%u", "%ld", "%lu", "%lld", "%llu", "%f", "%f", or "%Lf", respectively, where buf designates an internal character buffer of sufficient size.

There are also the following that go the other way around:

[n3290: 21.5/1, 21.5/4]:

int stoi(const string& str, size_t *idx = 0, int base = 10);
long stol(const string& str, size_t *idx = 0, int base = 10);
unsigned long stoul(const string& str, size_t *idx = 0, int base = 10);
long long stoll(const string& str, size_t *idx = 0, int base = 10);
unsigned long long stoull(const string& str, size_t *idx = 0, int base = 10);
float stof(const string& str, size_t *idx = 0);
double stod(const string& str, size_t *idx = 0);
long double stold(const string& str, size_t *idx = 0);

However, there's nothing generic that you can use (at least not until TR2, maybe!), and nothing at all in C++03.

这篇关于std :: lexical_cast - 有这样的事吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆