77

C++ 標準ライブラリはこの関数を定義していますか、それとも Boost に頼る必要がありますか?

ウェブで検索してもBoost以外は見つからなかったのですが、ここで質問したほうがいいと思いました。

4

5 に答える 5

93

部分的にのみ。

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引数の値の文字表現を保持するオブジェクトを返します。このオブジェクトは、それぞれ 、、、、、、、、、または の形式指定子を使用して呼び出すことによって生成されます。ここで、は十分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 にはまったくありません。

于 2011-11-09T13:03:27.267 に答える
19

いいえ、C++11 でもそうではありませんが、標準ライブラリ拡張の次のセットである Technical Report 2 に含めることが提案されています。

于 2011-11-09T13:05:20.173 に答える
10

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;
}
于 2011-11-09T13:14:08.137 に答える
6

いいえ、純粋なブーストのみです。

于 2011-11-09T13:05:30.303 に答える
5

ブーストが必要ない場合は、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"
于 2014-12-26T23:54:02.723 に答える