gpt4 book ai didi

c++ - 为什么这两个 sregex_iterator 语句之间存在差异?

转载 作者:搜寻专家 更新时间:2023-10-31 02:18:00 24 4
gpt4 key购买 nike

这是第一条语句:

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

最佳答案

在这一行中:

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

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

参见:

关于c++ - 为什么这两个 sregex_iterator 语句之间存在差异?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34971859/

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