Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/extjs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ c++;:ptr_fun()错误消息_C++ - Fatal编程技术网

C++ c++;:ptr_fun()错误消息

C++ c++;:ptr_fun()错误消息,c++,C++,下面是简单的测试程序。它尝试查找(案例1)并删除(案例2)字符串中的第一个空格。以下是测试结果: 1) 第1行导致“调用查找if(…)时没有匹配函数”错误。这是因为它试图查找std::isspace(),而不是全局isspace()。所以第2行是可以的 2) 如果将isspace()函数包装为ptr\u-fun(),如第3行和第4行,第3行将导致“调用ptr\u-fun(…)”错误,第4行正常 3) 第5行导致“调用not1(…)”错误。第6行生成大量错误消息,包括“调用(std::一元否定)(

下面是简单的测试程序。它尝试查找(案例1)并删除(案例2)字符串中的第一个空格。以下是测试结果:

1) 第1行导致“调用
查找if(…)
时没有匹配函数”错误。这是因为它试图查找
std::isspace()
,而不是全局
isspace()
。所以第2行是可以的

2) 如果将
isspace()
函数包装为
ptr\u-fun()
,如第3行和第4行,第3行将导致“调用
ptr\u-fun(…)
”错误,第4行正常

3) 第5行导致“调用
not1(…)
”错误。第6行生成大量错误消息,包括“调用
(std::一元否定)(char&)
”之类的内容

4) 通过使用ptr_fun()包装,第7行和第8行都可以,无论是全局还是std
isspace()

因此:

1) 为什么案例2中需要
ptr\u fun()
,而案例1中不需要

2) 为什么
std::isspace()
在案例2中与
ptr\u fun()
一起工作,而在案例1中不工作?如何解析
isspace()

3)
ptr\u fun()
(和其他类似的STL函数)的真正目的是什么?它是如何工作的

谢谢

#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
    string s = " abc";

// case 1 (find the first space):
//  find_if(s.begin(), s.end(), isspace);               // line 1 (error)
//  find_if(s.begin(), s.end(), ::isspace);             // line 2 (ok)

//  find_if(s.begin(), s.end(), ptr_fun(isspace));      // line 3 (error)
//  find_if(s.begin(), s.end(), ptr_fun(::isspace));    // line 4 (ok)

// case 2 (trim leading spaces):
//  s.erase(s.begin(), find_if(s.begin(), s.end(), not1((isspace))));   // line 5 (error)
//  s.erase(s.begin(), find_if(s.begin(), s.end(), not1((::isspace)))); // line 6 (error)

//  s.erase(s.begin(), find_if(s.begin(), s.end(), not1(ptr_fun<int, int>(::isspace))));    // line 7 (ok)
//  s.erase(s.begin(), find_if(s.begin(), s.end(), not1(ptr_fun<int, int>(std::isspace)))); // line 8 (ok)

    return 0;
}
#包括
#包括
使用名称空间std;
int main()
{
字符串s=“abc”;
//案例1(找到第一个空格):
//查找_if(s.begin()、s.end()、isspace);//第1行(错误)
//查找_if(s.begin()、s.end(),::isspace);//第2行(确定)
//查找if(s.begin()、s.end()、ptr_fun(isspace));//第3行(错误)
//查找if(s.begin(),s.end(),ptr_fun(::isspace));//第4行(确定)
//案例2(修剪前导空间):
//s.erase(s.begin(),find_if(s.begin(),s.end(),not1((isspace));//第5行(错误)
//s.erase(s.begin(),find_if(s.begin(),s.end(),not1((::isspace));//第6行(错误)
//s.erase(s.begin()、find_if(s.begin()、s.end()、not1(ptr_fun(::isspace));//第7行(确定)
//s.erase(s.begin()、find_if(s.begin()、s.end()、not1(ptr_fun(std::isspace)));//第8行(确定)
返回0;
}

首先,在使用前需要包含
。但执行此操作后,第1行仍可能出错,因为调用不明确,因为
中提供的重载可能包含在您包含的其他标准库标题中。无需
ptr\u fun
来消除歧义,只需使用
static\u cast

find_if(s.begin(), s.end(), static_cast<int(*)(int)>(isspace));
s.erase(s.begin(), find_if(s.begin(), s.end(), [](char c) {return !isspace(c)}));