这段代码中的std :: remove

编程入门 行业动态 更新时间:2024-10-03 08:30:31
这段代码中的std :: remove_if有什么问题(what's wrong with std::remove_if in this code)

solution.cc:14:47:错误:没有匹配函数来调用'remove_if(std :: basic_string :: iterator,std :: basic_string :: iterator,)'

#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; }

solution.cc:14:47: error: no matching function for call to 'remove_if(std::basic_string::iterator, std::basic_string::iterator, )'

#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() :error:没有匹配函数用于调用'remove_if(std :: basic_string :: iterator,std :: basic_string :: iterator,unresolved overloaded function type)'

调用std::transform时没有发生这种情况,因为你实际上指定了你正在使用哪个tolower 。

在全局命名空间和命名空间std都定义了isspace ,因此当你using namespace std并编写isspace ,就会产生歧义。 具体来说, <cctype>有一个重载, <locale>有另一个重载。

只要写::isspace就可以了。

I tried compiling your code in g++ 4.9.1. The exact error is:

In function int main(): error: no matching function for call to 'remove_if(std::basic_string::iterator, std::basic_string::iterator, unresolved overloaded function type)'

This didn't happen with your call to std::transform because you actually specified which tolower you're using.

isspace is defined both in the global namespace and in namespace std, so when you have using namespace std, and you write isspace, there's an ambiguity. To be specific, there's one overload in <cctype> and another defined in <locale>.

Just write ::isspace and you're good to go.

更多推荐

本文发布于:2023-08-04 20:56:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1422230.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:这段   代码   remove   std

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!