我安装了Visual Studio 2015 Update 2 Release Candidate,现在似乎遇到了一个使用sregex_token_iterator
的问题,目前为止似乎工作正常。为了验证我试图从cppreference.com下面的示例代码(请注意,我改变了可变text
有在最后一个空格):Visual Studio 2015 Update 2中的regex_token_iterator问题RC
#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实现中的一个bug或者是regex_token_iterator
的例子错误?
cppreference.com示例严格遵守标准,错误发生在C运行时而不是抛出异常,所以我确信它是STL实现中的一个错误。至少MSVC开发者非常聪明,可以为这个预期的情况添加一个断言。 – Youka
我建议使用boost正则表达式,STL正则表达式实现通常是buggy(至少对我来说),而切换到boost只需要改变命名空间。 – Youka