Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/159.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.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++ regex_匹配作为谓词_C++_Regex_C++11_Predicate - Fatal编程技术网

C++ regex_匹配作为谓词

C++ regex_匹配作为谓词,c++,regex,c++11,predicate,C++,Regex,C++11,Predicate,我试图使用std::regex_match()作为类成员函数中std::count_if()元素的谓词。但我不知道如何正确地将第二个参数(regex值)绕过到函数中 有没有一种方法可以使用std::regex_match()作为谓词(例如std::bind1st() 例如: int GetWeight::countWeight( std::regex reg ) { std::cout << std::count_if( word.begin(), word.end(),

我试图使用
std::regex_match()
作为类成员函数中
std::count_if()
元素的谓词。但我不知道如何正确地将第二个参数(regex值)绕过到函数中

有没有一种方法可以使用
std::regex_match()
作为谓词(例如
std::bind1st()

例如:

int GetWeight::countWeight( std::regex reg )
{
    std::cout << std::count_if( word.begin(), word.end(), 
                                std::bind1st( std::regex_match(), reg ) );
    return 1;
}
int GetWeight::countWeight(std::regex reg)
{

std::cout下面是一个示例,如果
std::count\u的谓词中有lambda,您可以如何执行此操作:

using Word = std::string;
using WordList = std::vector< Word >;

int countWeight( const WordList& list, const std::regex& re )
{
    return std::count_if( list.cbegin(), list.cend(), [&re]( const Word& word )
    {
        std::smatch matches;
        return std::regex_match( word, matches, re );
    });
};
使用Word=std::string;
使用WordList=std::vector;
int countWeight(const WordList和list,const std::regex和re)
{
返回std::count_if(list.cbegin()、list.cend()、[&re](常量字和字)
{
std::smatch匹配;
返回std::regex_match(word、matches、re);
});
};

下面是一个示例,如果
std::count\u的谓词中有lambda,您可以如何执行此操作:

using Word = std::string;
using WordList = std::vector< Word >;

int countWeight( const WordList& list, const std::regex& re )
{
    return std::count_if( list.cbegin(), list.cend(), [&re]( const Word& word )
    {
        std::smatch matches;
        return std::regex_match( word, matches, re );
    });
};
使用Word=std::string;
使用WordList=std::vector;
int countWeight(const WordList和list,const std::regex和re)
{
返回std::count_if(list.cbegin()、list.cend()、[&re](常量字和字)
{
std::smatch匹配;
返回std::regex_match(word、matches、re);
});
};

如果签名不完全匹配,就不能直接插入..虽然下面的答案似乎很好。如果签名不完全匹配,就不能直接插入..虽然下面的答案似乎很好。我还有一个诀窍,就是使用函子作为表达式。@Alex_H:嗯,使用lambda可以避免编写所有的样板文件你可以自己编写代码。;)Ofc,但函子可以更具可移植性。谢谢你,Azem。@Alex_H:不客气!:)你能详细介绍一下函子与使用lambda的“可移植性”吗?好吧,lambda作为语言的一部分,和语言的任何其他功能一样具有可移植性。而且,使用lambda可以提供更好的“局部性”与一个函子相比,它看起来更优雅,代码的编写和维护可以在同一个地方处理。相对而言,使用一个函数写代码和维护代码时,编写的代码和维护工作量都会增加。我非常感谢你的输入。谢谢。我指的是不同的C++标准。我知道,lambda是C++ 11中的。我认为您可以满足需要支持的遗留代码。我完全同意您在代码中使用lambda的理由。此功能使开发更简洁、更容易。我还介绍了使用函子作为表达式的诀窍。@Alex_H:嗯,使用lambda可以避免您自己编写所有样板代码。;)Ofc,但函子可以是more-portable。谢谢你,Azeem。@Alex_H:不客气!:)你能详细介绍一下函子与使用lambda的“可移植性”吗?好吧,lambda作为语言的一部分,和语言的任何其他功能一样可移植。而且,使用lambda可以提供更好的“局部性”与一个函子相比,它看起来更优雅,代码的编写和维护可以在同一个地方处理。相对而言,使用一个函数写代码和维护代码时,编写的代码和维护工作量都会增加。我非常感谢你的输入。谢谢。我指的是不同的C++标准。我知道,lambda是C++ 11中的。我认为您可以满足您需要支持的遗留代码。我完全同意您在代码中使用lambda的理由。此功能使开发更干净、更容易。