本文介绍了remove_if在尝试删除空格时抛出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从字符串中删除空格。但是有一个错误。
I am trying to delete white spaces from the string. but got throw an error.
我的代码做错了什么参数
Which parameter did my code do wrong.. thanks for looking
函数
#include <algorithm>
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <sstream>
using namespace std;
int main()
{
string myText;
myText = readText("file.txt");
myText.erase(remove_if(myText.begin(), myText.end(), isspace), myText.end());
cout << myText << endl;
return 0;
}
下面是我尝试编译时出现的错误。
Below is the error appear when I try compile.
encrypt.cpp: In function ‘int main()’:
encrypt.cpp:70:70: error: no matching function for call to ‘remove_if(std::basic_string<char>::iterator, std::basic_string<char>::iterator, <unresolved overloaded function type>)’
encrypt.cpp:70:70: note: candidate is:
/usr/include/c++/4.6/bits/stl_algo.h:1131:5: note: template<class _FIter, class _Predicate> _FIter std::remove_if(_FIter, _FIter, _Predicate)
推荐答案
你得到这个错误,因为有两个名为 isspace
的函数。
You got this error because there are two functions with name isspace
.
-
定义在
locale
em>:
template<class charT>
bool std::isspace(charT ch, const locale& loc);
定义在 cctype
全局命名空间:
int isspace( int ch );
因此,如果要使用第二个函数,您有两种方法:
So, if you want to use second function, you have two ways:
- 不要使用
using namespace std
。 使用::
调用函数,
- Do not use
using namespace std
. I prefer it. Use
::
to call the function, defined in global namespace
remove_if(myText.begin(), myText.end(), ::isspace)
// ^^
这篇关于remove_if在尝试删除空格时抛出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!