Jua*_*ero 1 c++ tr1 std visual-studio-2010
Visual Studio 2010中此C++代码失败:
const sregex_iterator end;
for (sregex_iterator match(origString.begin(), origString.end(), regex(regExPattern)); match != end; ++match)
{
useMatch(*match);
}
Run Code Online (Sandbox Code Playgroud)
在第一个循环之后,在第一个迭代器increment(operator++)中,调试器失败,表明regex_iterator是"孤立的".
我注意到可疑的正则表达式构造函数(我从某处复制了片段),我尝试了这个:
const sregex_iterator end;
regex regexObj(regExPattern);
for (sregex_iterator match(origString.begin(), origString.end(), regexObj); match != end; ++match)
{
useMatch(*match);
}
Run Code Online (Sandbox Code Playgroud)
这非常有效.
但是,为什么第一次尝试失败了?我认为它必须与for范围或可能与内联构造函数以及迭代器构造函数中的regex参数是引用的事实...
但是,正如我前一段时间在stackoverflow中读到的那样,我只记得我理解的东西,并且我想知道在C++中使用构造函数作为函数参数是否安全(new当然不使用).