std::remove_if 和 std::isspace - 编译时错误

std::remove_if and std::isspace - compile-time error

本文关键字:std 编译时错误 isspace if remove      更新时间:2023-10-16

我有以下代码:

#include <algorithm>
#include <cctype>
#include <string>
int main()
{
std::string str;
str.erase(std::remove_if(str.begin(), str.end(), std::isspace), str.end());
}

MSVC-11.0 编译此代码没有任何错误,但 gcc 4.7.2 给了我以下错误:

main.cpp: In function ‘int main()’:
main.cpp:8:66: error: no matching function for call to ‘remove_if(std::basic_string<char>::iterator, std::basic_string<char>::iterator, <unresolved overloaded function type>)’
main.cpp:8:66: note: candidate is:
In file included from /usr/include/c++/4.7/algorithm:63:0,
from main.cpp:1:
/usr/include/c++/4.7/bits/stl_algo.h:1160:5: note: template<class _FIter, class _Predicate> _FIter std::remove_if(_FIter, _FIter, _Predicate)
/usr/include/c++/4.7/bits/stl_algo.h:1160:5: note:   template argument deduction/substitution failed:
main.cpp:8:66: note:   couldn't deduce template parameter ‘_Predicate’

我发现了这个问题,但根据 cpp 首选项,这个函数的任何版本都没有接受两个参数。我也发现了这个问题,但是根据cpp首选项(是的,再次),我看到只有一个std::isspace函数重载。

谁是对的?我做错了什么?我该如何解决它?

还有另一个重载std::isspace,所以你需要指定使用哪一个。一个简单的方法是使用 lambda(或者如果您没有 C++11 支持,则编写自己的单行函数):

std::remove_if(str.begin(), str.end(), 
[](char c){ 
return std::isspace(static_cast<unsigned char>(c));
});

std::isspace是一个重载函数,尽管这两个重载驻留在不同的标头中。另请注意,您的代码可能会引入未定义的行为,因为只有范围0..UCHAR_MAX中的值才能传递给std::isspace,而char可能是有符号的。

这是一个解决方案:

std::string str;
auto f = [](unsigned char const c) { return std::isspace(c); };
str.erase(std::remove_if(str.begin(), str.end(), f), str.end());

以下解决方案应该可以帮助您解决编译时错误:

str.erase(std::remove_if(str.begin(), str.end(), (int(*) (int)) std::isspace), str.end());

C++ 11 之后,可以使用 lamda 函数(更容易理解),见下文:

string s = " 3/  2";
auto isSpace = [](const unsigned char c) 
{
return std::isspace(c);
};
s.erase(remove_if(s.begin(), s.end(), isSpace), s.end());

输出:

3/2