gpt4 book ai didi

c++ - Visual Studio 2015 Update 2 RC 中的 regex_token_iterator 问题

转载 作者:行者123 更新时间:2023-11-30 05:30:14 25 4
gpt4 key购买 nike

我安装了 Visual Studio 2015 Update 2 Release Candidate,现在使用 sregex_token_iterator 似乎有问题,到目前为止似乎工作正常。为了验证我尝试了来自 cppreference.com 的以下示例代码(请注意,我将变量 text 更改为末尾有一个空格):

#include <iostream>
#include <regex>

int main() {
std::string text = "Quick brown fox ";
// tokenization (non-matched fragments)
// Note that regex is matched only two times: when the third value is obtained
// the iterator is a suffix iterator.
std::regex ws_re("\\s+"); // whitespace
std::copy(std::sregex_token_iterator(text.begin(), text.end(), ws_re, -1),
std::sregex_token_iterator(),
std::ostream_iterator<std::string>(std::cout, "\n"));
}

运行它会给出以下断言:

Debug Assertion Failed!

Program: C:\WINDOWS\SYSTEM32\MSVCP140D.dll
File: c:\program files (x86)\microsoft visual studio 14.0\vc\include\xstring
Line: 247

Expression: string iterators incompatible

For information on how your program can cause an assertion
failure, see the Visual C++ documentation on asserts.

这是 Visual Studio STL 实现中的错误还是 regex_token_iterator 的示例有误?

最佳答案

对此感到抱歉 - 作为我们在 <regex> 中进行的性能修复的一部分对于 Update 2,我们不再制作大量临时字符串对象;如果 sub_match实例不匹配某些东西,我们只是制作值初始化的迭代器,它的行为“就好像”有一个空字符串匹配。

这个程序从 C++14 开始应该是有效的(并且道德上是 regex_token_iterator 中发生的事情);参见“null forward iterators”:

#include <stdio.h>
#include <string>

int main() {
// Value constructed iterators conceptually point to an empty container
std::string::iterator beginIt{};
std::string::iterator endIt{};
printf("This should be zero: %zu\n", endIt - beginIt);
}

...但是我们的调试断言禁止这样做。 regex_token_iterator恰好被它绊倒了。

请注意,在发布版本中(关闭调试断言)这一切都可以正常工作;该错误存在于迭代器调试机制中,而不存在于迭代器的行为中。

此错误将在 2015 Update 2 RTM 中修复。

关于c++ - Visual Studio 2015 Update 2 RC 中的 regex_token_iterator 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36057955/

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