8

次のエラーが表示されます:
prog.cpp: In member function 'void Sequence::GetSequence()':
prog.cpp:45: error: 'itoa' はこのスコープで宣言されていません

cstdlib ヘッダー ファイルをインクルードしましたが、機能しません。

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <functional>
using namespace std;

template<typename T>
struct predicate :
public binary_function<T, T, bool>
{
    bool operator() (const T& l,const T &r) const
    {
          return l < r;
    }
};

class Sequence
{
    public:
    Sequence(vector<string> &v)
    { /* trimmed */ }

void GetSequence(void)
{
    string indices = "";
    char buf[16];

    for( map<int, string>::iterator
            i = m.begin(); i != m.end(); ++i )
    {
indices = indices
                  + string(itoa((i->first), buf, 10));
    }

    SortedSequence("", indices);
}

// --- trimmed ---
4

3 に答える 3

14

標準にはありませんitoaが、C++11 ではstd::to_string関数を使用できます。

于 2012-06-16T12:32:59.063 に答える
7

C++11 では、 を使用できますstd::to_string。これが利用できない場合は、次を使用できますstd::stringstream

std::stringstream ss; int x = 23;
ss << x;
std::string str = ss.str();

これが冗長すぎる場合は、boost::lexical_castがあります。lexical_castとのパフォーマンスについていくつかの不満があるstd::stringstreamため、これが重要な場合は注意してください。

別のオプションは、Spirit のサブライブラリであるBoost.Karmaを使用することです。ほとんどのベンチマークで優れています。

通常、itoa悪い考えです。これは、C または C++ 標準の一部ではありません。それをサポートするプラットフォームを特定し、条件付きで使用するように注意することはできますが、より良い解決策があるのに、そうする必要はありません。

于 2012-06-16T12:42:57.770 に答える