c++ - 'itoa' : function does not take 1 arguments & 'c' : undeclared identifier

标签 c++ string itoa

我已经尝试了 2 天来让这段代码正常工作。只是一个又一个错误。

谁能指出我做错了什么?

#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;

int main()
{
    int h = 0;
    for(int a = 100; a<1000; a++)
        for(int b = 100; b<1000; b++)
            int c = a * b;
// Error: "c" is undefined
            if ((c == reverse(c)) && (c > h))
                h = c;
    cout << "The answer is: " << h << endl;
}

int reverse (int x)
{
// Error: "'itoa' : function does not take 1 arguments"
    string s = string(itoa(x));
    reverse(s.begin(), s.end());
  return (x);
}

使用 std::to_string 只会给我更多的错误。

最佳答案

当您的编译器在错误消息中向您解释某些内容时,您应该相信它。事实上,itoa 接受多个参数,您可以在以下链接中看到:

http://www.cplusplus.com/reference/clibrary/cstdlib/itoa/

编辑:哦,顺便说一句,这可以使用标准的 C++ 样式代码实现(根据评论中的建议修复了一些代码):

int reverse(int x)
{
    std::stringstream ss;
    ss << x;

    std::string s = ss.str();
    std::reverse(s.begin(), s.end());

    ss.clear();
    ss.str(s.c_str());

    ss >> x;

    return x;
}

这里。不确定这是最干净的解决方案,但它适用于我的编译器。

编辑:在这里找到了如何只使用一个字符串流:How to clear stringstream?

关于c++ - 'itoa' : function does not take 1 arguments & 'c' : undeclared identifier,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11706692/

相关文章:

c++ - 我在实现 Prim 最小生成树算法时的逻辑错误是什么?

c# - 是否有与 C# 的 Vector3 等效的标准 C++?

c++ - 带 boost 的最小生成树

javascript - 有没有什么好的方法可以在rest api中传递错误对象

javascript - 从格式化字符串中提取变量

将无符号整数 (uint16_t) 转换为字符串。标准 itoa base 10 给出负值

itoa() 的 C++ 标准替代方法,用于将 int 转换为 base 10 char*

c++ - 如何添加一个多位数的 int 和一个 char,输出将是 int + char

c++ - 用 C++ 对数字进行排序

java - 为什么空格在该分割指令中显示为子字符串?