[英]std::lexical_cast - is there such a thing?
C ++標准庫是否定義了此功能,還是我必須訴諸Boost?
我在網上搜索,除了Boost以外什么都找不到,但是我想最好在這里問一下。
僅部分。
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);
返回值:每個函數都返回一個
string
對象,其中包含其參數值的字符表示,該string
值可通過調用格式為"%d"
,"%u"
,"%ld"
"%u"
sprintf(buf, fmt, val)
生成"%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庫擴展。
沒有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;
}
不,這只是純粹的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()
官方頁面上有更多示例。
按位置訪問參數:
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"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.