std::lexical_cast - is there such a thing?(std::lexical_cast - 有这样的事情吗?)
问题描述
C++ 标准库是否定义了这个函数,还是我必须求助于 Boost?
Does the C++ Standard Library define this function, or do I have to resort to Boost?
我在网上搜索,除了 Boost 什么都找不到,但我想我最好在这里问一下.
I searched the web and couldn't find anything except Boost, but I thought I'd better ask here.
推荐答案
仅部分.
C++11
有 std::to_string
用于内置类型:
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);
返回: 每个函数返回一个 string
对象,其中包含其参数值的字符表示通过使用格式调用 sprintf(buf, fmt, val)
生成"%d"
、"%u"
、"%ld"
、"%lu"
的说明符,"%lld"
, "%llu"
,分别为 "%f"
、"%f"
或 "%Lf"
,其中 buf
表示足够大小的内部字符缓冲区.
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.
还有以下相反的情况:
[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);
然而,没有任何通用的东西可以使用(至少 不是直到 TR2,也许吧!),而在 C++03 中什么都没有.
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 - 有这样的事情吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:std::lexical_cast - 有这样的事情吗?


基础教程推荐
- 从 std::cin 读取密码 2021-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 使用从字符串中提取的参数调用函数 2022-01-01