我已经尝试了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;
}
这里。不确定这是最干净的解决方案,但可以在我的编译器上使用。
编辑:在这里找到如何仅使用一个stringstream:How to clear stringstream?
关于c++ - 'itoa':函数不接受1个参数& 'c':未声明的标识符,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11706692/