gpt4 book ai didi

c++ - regex_token_iterator 和 regex_iterator 有什么区别?

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

regex_token_iterator 和 regex_iterator 有区别吗?

看起来他们都做同样的工作,但不确定哪一个性能更好?

最佳答案

两者之间确实存在差异,如果我们查看 cppreference,它描述了 std::regex_iterator如下:

std::regex_iterator is a read-only ForwardIterator that accesses the individual matches of a regular expression within the underlying character sequence.

std::regex_token_iterator作为:

std::regex_token_iterator is a read-only ForwardIterator that accesses the individual sub-matches of every match of a regular expression within the underlying character sequence. It can also be used to access the parts of the sequence that were not matched by the given regular expression (e.g. as a tokenizer).

因此 std::regex_token_iterator 还允许您匹配未匹配的标记或 n-th 子表达式。

我在上面链接的 std::regex_token_iterator 的 cppreference 部分描述了一个典型的实现,如下所示:

A typical implementation of std::regex_token_iterator holds the underlying std::regex_iterator, a container (e.g. std::vector) of the requested submatch indexes, the internal counter equal to the index of the submatch, a pointer to std::sub_match, pointing at the current submatch of the current match, and a std::match_results object containing the last non-matched character sequence (used in tokenizer mode).

本书The C++ Standard Library 14.4 Regex Token Iterators 解释如下:

A regex iterator helps to iterate over matched subsequences. However, sometimes you also want to process all the contents between matched expressions. [...] In addition, you can specify a list of integral values, which represent elements of a “tokenization”:

  • -1 means that you are interested in all the subsequences between matched regular expressions (token separators).
  • 0 means that you are interested in all the matched regular expressions (token separators).
  • Any other value n means that you are interested in the matched nth subexpression inside the regular expressions.

图书站点提供了 sregex_token_iterator 的示例代码和 sregex_iterator这也应该有所帮助。

关于c++ - regex_token_iterator 和 regex_iterator 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26320987/

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