C+中谓词的逻辑否定+; 我遵循了加速C++,编写了一个函数,把一个字符串分割成一个单词的向量(空间字符分隔), FordION,如果被使用。 vector<string> split(const string& str) { typedef string::const_iterator iter; vector<string> ret; iter i = str.begin(); while (i != str.end()) { i = find_if(i, str.end(), not_space); iter j = find_if(i, str.end(), space); if (i != str.end()) ret.push_back(string(i, j)); i = j; } return ret; }

C+中谓词的逻辑否定+; 我遵循了加速C++,编写了一个函数,把一个字符串分割成一个单词的向量(空间字符分隔), FordION,如果被使用。 vector<string> split(const string& str) { typedef string::const_iterator iter; vector<string> ret; iter i = str.begin(); while (i != str.end()) { i = find_if(i, str.end(), not_space); iter j = find_if(i, str.end(), space); if (i != str.end()) ret.push_back(string(i, j)); i = j; } return ret; },c++,C++,这里是否需要编写两个独立的谓词,或者一个谓词可以简单地传递!空格代替非空格?只需使用std::not1(std::ptr_-fun(space))std::not1在中声明 (还有一个std::not2用于二进制谓词;std::not1用于一元谓词。)只需使用std::not1(std::ptr_fun(space))std::not1在中声明 (还有一个std::not2用于二进制谓词;std::not1用于一元谓词。)只需使用std::not1(std::ptr_fun(space))std

这里是否需要编写两个独立的谓词,或者一个谓词可以简单地传递
!空格
代替
非空格

只需使用
std::not1(std::ptr_-fun(space))
<代码>std::not1在
中声明

(还有一个
std::not2
用于二进制谓词;
std::not1
用于一元谓词。)

只需使用
std::not1(std::ptr_fun(space))
<代码>std::not1在
中声明

(还有一个
std::not2
用于二进制谓词;
std::not1
用于一元谓词。)

只需使用
std::not1(std::ptr_fun(space))
<代码>std::not1在
中声明

(还有一个
std::not2
用于二进制谓词;
std::not1
用于一元谓词。)

只需使用
std::not1(std::ptr_fun(space))
<代码>std::not1在
中声明

(还有一个
std::not2
用于二进制谓词;
std::not1
用于一元谓词。)

您也可以声明

template <bool find_space> bool space(char c) {
    return find_space ^ (!isspace(c));
}
模板布尔空间(字符c){
返回find_space^(!isspace(c));
}
然后在
find_if()
的参数中将其称为
space
space
。与std::not1()相比,它的功能更为广泛。您还可以声明

template <bool find_space> bool space(char c) {
    return find_space ^ (!isspace(c));
}
模板布尔空间(字符c){
返回find_space^(!isspace(c));
}
然后在
find_if()
的参数中将其称为
space
space
。与std::not1()相比,它的功能更为广泛。您还可以声明

template <bool find_space> bool space(char c) {
    return find_space ^ (!isspace(c));
}
模板布尔空间(字符c){
返回find_space^(!isspace(c));
}
然后在
find_if()
的参数中将其称为
space
space
。与std::not1()相比,它的功能更为广泛。您还可以声明

template <bool find_space> bool space(char c) {
    return find_space ^ (!isspace(c));
}
模板布尔空间(字符c){
返回find_space^(!isspace(c));
}

然后在
find_if()
的参数中将其称为
space
space
。比
std::not1()
多功能您不能简单地使用
!空格
而不是
非_空格
,因为在这种情况下,您所要做的就是将
false
传递给
find_if
。这是因为
space
将衰减为指向函数的指针,并且函数指针可以隐式转换为
bool
。应用
到布尔值将始终导致
false
(因为函数指针永远不会是
nullptr

您可以通过将函数
空间
包装到中来重用它,这将否定传递给它的谓词的结果。不幸的是,它并不像编写
std::not1(space)
那样简单,因为
not1
要求谓词定义一个名为
参数类型
的嵌套类型,而您的谓词不满足该类型

若要将函数转换为可与
not1
一起使用的谓词,必须首先将其封装。因此,
split
函数中的行变为:

i = find_if(i, str.end(), std::not1(std::ptr_fun(space)));

使用C++11,不需要使用
not1
ptr_-fun
sheanigans,只需使用lambda表达式:

i = find_if(i, str.end(), [](char c) {return !space(c);});

您不能简单地使用
!空格
而不是
非_空格
,因为在这种情况下,您所要做的就是将
false
传递给
find_if
。这是因为
space
将衰减为指向函数的指针,并且函数指针可以隐式转换为
bool
。应用
到布尔值将始终导致
false
(因为函数指针永远不会是
nullptr

您可以通过将函数
空间
包装到中来重用它,这将否定传递给它的谓词的结果。不幸的是,它并不像编写
std::not1(space)
那样简单,因为
not1
要求谓词定义一个名为
参数类型
的嵌套类型,而您的谓词不满足该类型

若要将函数转换为可与
not1
一起使用的谓词,必须首先将其封装。因此,
split
函数中的行变为:

i = find_if(i, str.end(), std::not1(std::ptr_fun(space)));

使用C++11,不需要使用
not1
ptr_-fun
sheanigans,只需使用lambda表达式:

i = find_if(i, str.end(), [](char c) {return !space(c);});

您不能简单地使用
!空格
而不是
非_空格
,因为在这种情况下,您所要做的就是将
false
传递给
find_if
。这是因为
space
将衰减为指向函数的指针,并且函数指针可以隐式转换为
bool
。应用
到布尔值将始终导致
false
(因为函数指针永远不会是
nullptr

您可以通过将函数
空间
包装到中来重用它,这将否定传递给它的谓词的结果。不幸的是,它并不像编写
std::not1(space)
那样简单,因为
not1
要求谓词定义一个名为
参数类型
的嵌套类型,而您的谓词不满足该类型

若要将函数转换为可与
not1
一起使用的谓词,必须首先将其封装。因此,
split
函数中的行变为:

i = find_if(i, str.end(), std::not1(std::ptr_fun(space)));

使用C++11,不需要使用
not1
ptr_-fun
sheanigans,只需使用lambda表达式:

i = find_if(i, str.end(), [](char c) {return !space(c);});

您不能简单地使用
!空格
而不是
非空格
,因为您将在该ca中执行所有操作