此代码中的 std::remove_if 有什么问题

what's wrong with std::remove_if in this code

本文关键字:if 什么 问题 remove std 代码      更新时间:2023-10-16

solution.cc:1:47:错误:调用"remove_if(std::basic_string::迭代器,std::asic_string::迭代器("时没有匹配的函数

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

int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
    string s;
    std::getline(cin, s);
    std::transform(s.begin(), s.end(), s.begin(), ::tolower);
    std::remove_if(s.begin(), s.end(), isspace);
   // cout<<s;
    int len=s.length();
    len=len+90;
    bool temp;
    bool value[len];
    for(int i=0;i<len;i++)
        {
           int x=(int)s[i];
       if(value[x]!=1) value[x]=1;
    }
    for(int i=65;i<=90;i++)
     if(value[i]==1) {temp=true;continue;}
        else {temp=false;break;}
    if(temp) cout<<"pangram"<<endl;
    else cout<<"not pangram"<<endl;
    return 0;
}

我尝试在g++ 4.9.1中编译您的代码。准确的错误是:

在函数int main()中:错误:没有匹配的函数用于调用"remove_if(std::basic_string::迭代器,std::asic_string::迭代器,未解析的重载函数类型(">

您对std::transform的调用没有发生这种情况,因为您实际上指定了正在使用的tolower

isspace在全局名称空间和名称空间std中都有定义,所以当您有using namespace std,而您编写isspace时,会出现歧义。具体来说,<cctype>中有一个过载,<locale>中定义了另一个过载。

只要写::isspace就可以了。