C++ std::lexical_cast - 有这样的事情吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8065413/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 17:57:18  来源:igfitidea点击:

std::lexical_cast - is there such a thing?

c++parsingcastingstdc++-standard-library

提问by smallB

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

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

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

我在网上搜索,除了 Boost 什么都找不到,但我想我最好在这里问一下。

回答by Lightness Races in Orbit

Only partially.

只是部分。

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

C++11<string>std::to_string内置类型:

[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 stringobject 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 bufdesignates an internal character buffer of sufficient size.

[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指定具有足够的尺寸的内部字符缓冲区。

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

[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.

但是,没有什么通用的东西可以使用(至少在 TR2 之前不能使用,也许吧!),在 C++03 中也没有。

回答by CharlesB

No it isn't, even in C++11, but it's proposed for inclusionin Technical Report 2, the next set of std library extensions.

不,即使在 C++11 中也不是,但建议将其包含在技​​术报告 2 中,即下一组标准库扩展。

回答by luke

There's no std::lexical_cast, but you can always do something similar with stringstreams:

没有 std::lexical_cast,但你总是可以用stringstreams做类似的事情:

template <typename T>
T lexical_cast(const std::string& str)
{
    T var;
    std::istringstream iss;
    iss.str(str);
    iss >> var;
    // deal with any error bits that may have been set on the stream
    return var;
}

回答by Some programmer dude

No it's a pure Boost thing only.

不,这只是纯粹的 Boost 东西。

回答by Sandu Liviu Catalin

If you don't want boost then a lightweight library called fmtimplements the following:

如果你不想要 boost 那么一个名为fmt的轻量级库实现以下内容:

// Works with all the C++11 features and AFAIK faster then boost or standard c++11
std::string string_num = fmt::format_int(123456789).str(); // or .c_str()

More examples from the official page.

来自官方页面的更多示例。

Accessing arguments by position:

按位置访问参数:

format("{0}, {1}, {2}", 'a', 'b', 'c');
// Result: "a, b, c"
format("{}, {}, {}", 'a', 'b', 'c');
// Result: "a, b, c"
format("{2}, {1}, {0}", 'a', 'b', 'c');
// Result: "c, b, a"
format("{0}{1}{0}", "abra", "cad");  // arguments' indices can be repeated
// Result: "abracadabra"

Aligning the text and specifying a width:

对齐文本并指定宽度:

format("{:<30}", "left aligned");
// Result: "left aligned                  "
format("{:>30}", "right aligned");
// Result: "                 right aligned"
format("{:^30}", "centered");
// Result: "           centered           "
format("{:*^30}", "centered");  // use '*' as a fill char
// Result: "***********centered***********"

Replacing %+f, %-f, and % f and specifying a sign:

替换 %+f、%-f 和 % f 并指定符号:

format("{:+f}; {:+f}", 3.14, -3.14);  // show it always
// Result: "+3.140000; -3.140000"
format("{: f}; {: f}", 3.14, -3.14);  // show a space for positive numbers
// Result: " 3.140000; -3.140000"
format("{:-f}; {:-f}", 3.14, -3.14);  // show only the minus -- same as '{:f}; {:f}'
// Result: "3.140000; -3.140000"

Replacing %x and %o and converting the value to different bases:

替换 %x 和 %o 并将值转换为不同的基数:

format("int: {0:d};  hex: {0:x};  oct: {0:o}; bin: {0:b}", 42);
// Result: "int: 42;  hex: 2a;  oct: 52; bin: 101010"
// with 0x or 0 or 0b as prefix:
format("int: {0:d};  hex: {0:#x};  oct: {0:#o};  bin: {0:#b}", 42);
// Result: "int: 42;  hex: 0x2a;  oct: 052;  bin: 0b101010"