std :: lexical_cast - 有这样的事吗?

sma*_*llB 72 c++ parsing casting std c++-standard-library

C++标准库是否定义了此函数,还是必须使用Boost?

我搜索网络除了Boost之外找不到任何东西,但我想我最好问一下这里.

Lig*_*ica 85

只是部分.

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);
Run Code Online (Sandbox Code Playgroud)

返回:每个函数返回一个string物体保持,将通过调用来产生其自变量的值的字符表示sprintf(buf, fmt, val)与的格式说明"%d","%u","%ld","%lu","%lld","%llu", "%f","%f",或"%Lf",分别,其中buf指定具有足够的尺寸的内部字符缓冲区.

反过来也有以下方面:

[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);
Run Code Online (Sandbox Code Playgroud)

但是,没有任何通用的东西你可以使用(至少在TR2之前,也许!),而在C++ 03中什么都没有.

  • @mcheema:http://www.stroustrup.com/C++11FAQ.html#delegating-ctor然后.要么他意味着Boost,要么他将其用作任意抽象来达到目的,或者他是错的. (3认同)
  • @mcheema记住Stroustrup对代码使用粗体斜体比例字体的瘾,我不认为人们可以认真地谈论他的代码的风格._*玩笑*_ (3认同)
  • 嗯,实际上有[`stoi`](http://en.cppreference.com/w/cpp/string/basic_string/stol)和朋友:) (2认同)
  • @mcheema:我们都有兴趣传播良好的编码风格实践,但这并不会使它们变得不那么主观! (2认同)

Cha*_*esB 18

不,它不是,即使在C++ 11中,但它建议包含在技​​术报告2中,即下一组std库扩展.

  • 顺便说一句,C++ 17的状态是什么? (15认同)

luk*_*uke 12

没有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;
}
Run Code Online (Sandbox Code Playgroud)

  • 性能始终是一个问题,但它永远不是唯一的问题.简单,清晰,正确和可维护性也非常重要.我不会在紧密循环中使用上面的代码,但它可能适用于其他情况.关于SO的性能的一个常见问题是,如果你已经分析了你的代码,并发现牺牲其他问题是值得的:) (11认同)
  • 如果你最终使用`lexical_cast <string>(string)`这将只返回第一个单词,而不是你传入的单词.(你可以在模板化的函数或其他东西中使用它,而不是直接使用它.)需要注意的东西. (8认同)
  • 谢谢,但这比boost :: lexical_cast还要慢 (3认同)
  • 基于流的解决方案不会像snprintf那样快,但你没有在你的问题中提到性能问题. (3认同)
  • 通过使`iss`静态可以加速这个版本 (2认同)
  • @MattMcNabb,也许但是你每次都必须重置 ISS 的缓冲区,而且它不是线程安全的。这是一个简单的例子。让它成为傻瓜证明是读者/Boost 库的一个练习;) (2认同)

Som*_*ude 6

不,它只是一个纯粹的Boost东西.


San*_*lin 5

如果你不想要 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()
Run Code Online (Sandbox Code Playgroud)

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

按位置访问参数:

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"
Run Code Online (Sandbox Code Playgroud)

对齐文本并指定宽度:

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***********"
Run Code Online (Sandbox Code Playgroud)

替换 %+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"
Run Code Online (Sandbox Code Playgroud)

替换 %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"
Run Code Online (Sandbox Code Playgroud)