Lexical_cast ——有这种东西吗?

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

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

65136 次浏览

只有一部分。

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 string object holding the 其参数值的字符表示形式 通过使用格式调用 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);

然而,没有任何通用的东西可以使用(至少是 直到 TR2,也许!) ,在 C + + 03中也没有任何东西。

不,即使在 C + + 11中也不是,但在下一套标准库扩展技术报告2中是 建议列入

不,这是纯粹的推进的东西。

没有 std: : lexical _ cast,但是您总是可以对 弦流进行类似的操作:

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

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

// 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()

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

按立场查阅论点:

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"

对齐文本并指定宽度:

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***********"

替换% + 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"

替换% 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"