gpt4 book ai didi

c++ - regex_match 作为谓词

转载 作者:行者123 更新时间:2023-11-28 01:47:11 25 4
gpt4 key购买 nike

我正在尝试使用 std::regex_match()作为 std::count_if() 中的谓词与 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;
}

wordvector<std::string>我需要计算匹配 std::regex reg 的元素绕过课外。

最佳答案

这是一个示例,您可以如何在 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 );
});
};

关于c++ - regex_match 作为谓词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44558870/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com