回文过滤器,函数运行良好,但main()无法编译

Palindrome Filter, the functions work perfectly, but the main() doesn't compile

本文关键字:编译 main 函数 过滤器 运行 回文      更新时间:2023-10-16

编译器给我一个错误,我尝试了包括周期的所有内容,但它没有帮助,也许有人有一个想法?功能非常有效,但是主((部分不

#include <iostream>
#include<vector>
using namespace std;

bool IsPalindrom(string s) {
    for (size_t i = 0; i < s.size() / 2; ++i) {
        if (s[i] != s[s.size() - i - 1]) {
            return false;
        }
    }
    return true;
}
vector <string> PalindromFilter(vector<string> words, int min_Length){
 vector<string> result;
 for(auto s : words){
    if (s.size() >= min_Length && IsPalindrom(s)){
        result.push_back(s);
    }
 }
 return result;
}
int main(){

cout << PalindromFilter({"abacaba", "aba"}, 4);
}

请注意PalindromFilter()函数的返回类型。它返回字符串的向量。您不能使用cout直接打印向量的内容。

相反,您可以尝试存储返回的值并使用显示功能。您的参考代码:

// Function to display elements of the vector of strings
void Display(const vector<string> & vec)
{
    for (int i = 0; i < vec.size(); i++)
    {
        // Note that for printing a string you need to
        // use c_str() 
        cout << vec[i].c_str() << endl;
    }
}

在主要功能中:

int main() 
{
    vector<string> output = PalindromFilter({ "abacaba", "aba" }, 4);
    Display(output);
    return 0;
}
相关文章: