gpt4 book ai didi

c++ - 计算匹配数

转载 作者:可可西里 更新时间:2023-11-01 15:40:20 26 4
gpt4 key购买 nike

如何使用 C++11 的 std::regex 计算匹配项的数量?

std::regex re("[^\\s]+");
std::cout << re.matches("Harry Botter - The robot who lived.").count() << std::endl;

预期输出:

7

最佳答案

您可以使用 regex_iterator 生成所有匹配项,然后使用 distance 计算它们:

std::regex  const expression("[^\\s]+");
std::string const text("Harry Botter - The robot who lived.");

std::ptrdiff_t const match_count(std::distance(
std::sregex_iterator(text.begin(), text.end(), expression),
std::sregex_iterator()));

std::cout << match_count << std::endl;

关于c++ - 计算匹配数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8283735/

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