2016-01-24 82 views
2

这是第一个发言:这两个sregex_iterator语句之间为什么有区别?

string s("hello.world"); 
sregex_iterator pos(s.cbegin(), s.cend(), regex(R"(\.)")); 
sregex_iterator end; 
for (; pos != end; ++pos) 
    cout << pos->str() << endl; 

这是第二次声明:

string s("hello.world"); 
regex reg(R"(\.)"); 
sregex_iterator pos(s.cbegin(), s.cend(), reg); 
sregex_iterator end; 
for (; pos != end; ++pos) 
    cout << pos->str() << endl; 

第二程序可以正常运行,但第一中止在运行时。 enter image description here

+0

https://en.wiktionary.org/wiki/orphan –

+0

@LightnessRacesinOrbit可怜的小迭代:( – Drop

回答

2

在这一行:

sregex_iterator pos(s.cbegin(), s.cend(), regex(R"(\.)")); 

表达regex(R"(\.)")对应于正被后函数调用立即销毁临时对象。所有“指向”它的迭代器都将失效。尝试使用此迭代器会导致失败的调试运行时检查和断言被抛出。

参见:

+0

和错误消息说,这个迭代器有被孤儿。 –

相关问题