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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11063413/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 14:47:32  来源:igfitidea点击:

Error:‘itoa’ was not declared in this scope

c++

提问by Green goblin

I am getting the following error:
prog.cpp: In member function ‘void Sequence::GetSequence()':
prog.cpp:45: error: ‘itoa' was not declared in this scope

我收到以下错误:
prog.cpp: 在成员函数 'void Sequence::GetSequence()':
prog.cpp:45: 错误: 'itoa' 未在此范围内声明

I have include cstdlib header file but its not working.

我已经包含 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 ---

回答by juanchopanza

There's no itoain the standard, but in C++11 you can use the std::to_stringfunctions.

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

回答by pmr

In C++11 you can use std::to_string. If this is not available to you, you can use a std::stringstream:

在 C++11 中,您可以使用std::to_string. 如果这对您不可用,您可以使用std::stringstream

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

If this is too verbose, there is boost::lexical_cast. There are some complaints about the performance of lexical_castand std::stringstream, so watch out if this is important to you.

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

Another option is to use a Boost.Karma, a sublibrary of Spirit. It comes out ahead in most benchmarks.

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

Usually, itoais a bad idea. It is neither part of the C nor C++ standard. You can take some care to identify the platforms that support it and use it conditionally, but why should you, when there are better solutions.

通常,这itoa是一个坏主意。它既不是 C 也不是 C++ 标准的一部分。您可以谨慎地确定支持它的平台并有条件地使用它,但如果有更好的解决方案,为什么还要这样做。

回答by Vadzim

I've found that itoawas not available in MinGWGCC at least until 4.9.3 but isavailable in MinGW-w64GCC 5.3.0.

我发现itoa没有可用的MinGWGCC至少要等到4.9.3,但就是提供的MinGW-W64GCC 5.3.0。

It guess it's absense was related to broken vswprintfissue which also caused missing std::stoiand std::to_stringin MinGW.

它猜测它的缺席与损坏的 vswprintf问题有关,该问题也导致MinGW丢失。std::stoistd::to_string