regex_match 作为谓词
regex_match as predicate
我正在尝试将 std::regex_match()
用作 std::count_if()
中的谓词,并在 class 成员函数中使用 std::vector<string>
元素。但是不知道如何正确地将第二个参数(正则表达式值)绕过到函数中。
有没有办法使用 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;
}
word
是 vector<std::string>
,我需要在其中计算匹配 std::regex reg
的元素绕过 class.
这是一个如何在 std::count_if
的谓词中使用 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 );
});
};
我正在尝试将 std::regex_match()
用作 std::count_if()
中的谓词,并在 class 成员函数中使用 std::vector<string>
元素。但是不知道如何正确地将第二个参数(正则表达式值)绕过到函数中。
有没有办法使用 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;
}
word
是 vector<std::string>
,我需要在其中计算匹配 std::regex reg
的元素绕过 class.
这是一个如何在 std::count_if
的谓词中使用 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 );
});
};