Mil*_*d R 0 c++ stl set segmentation-fault
假设我们有这段代码:
set<int> s;
set<int>::iterator it = s.find(val);
s.erase(it);
Run Code Online (Sandbox Code Playgroud)
正如cplusplus.com所说,如果当时int val不存在set<int> s,s.find(val)将会返回set::end.
现在我的问题是,如果我们set::end转到那里会发生什么set::erase()?
是否可以接收诸如segmentation fault或之类的信号aborted并获得运行时错误?或者这个特例是在处理set?
在C++ 03中,std::set::erase()表单中定义了单个迭代器的行为,其中有以下假设(突出显示已添加):
在表69中,X是关联容器类,a是X的值,当X支持唯一键时,a_uniq是X的值,当X支持多个键时,a_eq是X的值,i和j满足输入迭代器要求并且引用value_type的元素,[i,j]是有效范围,p是a的有效迭代器,q是a的有效可解引用迭代器,[q1,q2)是a中的有效范围,t是值X :: value_type,k是X :: key_type的值,c是X :: key_compare类型的值.
表69说明了这个erase()功能:
a.erase(q)- 擦除指向的元素q
换句话说,迭代器必须是可解除引用的.如果它不是未定义的行为,因为没有保持前置条件.
关于图书馆以任何特定方式行事,没有任何承诺.某些库(例如MSVC)可以在某些配置中包含迭代器调试.例如,在VS 2012中使用调试配置运行时,您将看到以下内容:
---------------------------
Microsoft Visual C++ Runtime Library
---------------------------
Debug Assertion Failed!
Program: C:\Windows\system32\MSVCP110D.dll
File: c:\program files (x86)\microsoft visual studio 11.0\vc\include\xtree
Line: 1326
Expression: map/set erase iterator outside range
For information on how your program can cause an assertion
failure, see the Visual C++ documentation on asserts.
(Press Retry to debug the application)
---------------------------
Abort Retry Ignore
---------------------------
Run Code Online (Sandbox Code Playgroud)