错误:未在此范围内声明'itoa'

Gre*_*lin 8 c++

我收到以下错误:
prog.cpp:在成员函数'void Sequence :: GetSequence()':
prog.cpp:45:错误:'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 ---
Run Code Online (Sandbox Code Playgroud)

jua*_*nza 14

itoa标准中没有,但在C++ 11中,您可以使用std :: to_string函数.

  • 是的,但你不能像`itoa`那样用`std :: to_string`指定基数 (10认同)

pmr*_*pmr 7

在C++ 11中,您可以使用std::to_string.如果您无法使用此功能,则可以使用std::stringstream:

std::stringstream ss; int x = 23;
ss << x;
std::string str = ss.str();
Run Code Online (Sandbox Code Playgroud)

如果这太冗长了,那就有boost :: lexical_cast.还有一些大概的性能的抱怨lexical_caststd::stringstream,所以看出来,如果这对你很重要.

另一个选择是使用Boost.Karma,一个Spirit的子库.它在大多数基准测试中都是领先的.

通常,这itoa是一个坏主意.它既不是C也不是C++标准的一部分.您可以小心识别支持它的平台并有条件地使用它,但是当有更好的解决方案时,为什么要这样做.