我编写了一段代码来检查回文,但它在命令提示符和在线编译器上给出了错误,但我在不同的在线编译器上执行了代码,它成功编译并给出了理想的结果。 错误是:palindrome.cpp:9:26:错误:“size”未在此范围内声明
#include<iostream>
using namespace std;
int main()
{
string num_str = "";
cin >> num_str;
string new_str = "";
for(int x = (size(num_str)-1); x >= 0; x--){
new_str += num_str[x];
}
cout << (num_str == new_str ? "palindrome" : "Non-palindrome");
return 0;
}```
要获取字符串的大小,可以使用成员函数。更改此行:
for(int x = (size(num_str)-1); ... )
到
for(int x = num_str.size()-1; ... )
此外,您可能应该包括:
#include <iostream>
#include <string>
// ...
size() 仅在 c++17 上可用。检查您的编译器标准。