palindrome.cpp:9:26:错误:“size”未在此范围内声明

问题描述 投票:0回答:2

我编写了一段代码来检查回文,但它在命令提示符和在线编译器上给出了错误,但我在不同的在线编译器上执行了代码,它成功编译并给出了理想的结果。 错误是: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;
}``` 
c++ string scope size palindrome
2个回答
0
投票

要获取字符串的大小,可以使用成员函数。更改此行:

for(int x = (size(num_str)-1); ... )

for(int x = num_str.size()-1; ... )

此外,您可能应该包括:

#include <iostream>
#include <string>
// ...

0
投票

size() 仅在 c++17 上可用。检查您的编译器标准。

© www.soinside.com 2019 - 2024. All rights reserved.