vec.erase(vec.end());合法的?
vec.erase(vec.end()); Legal?
顺序容器可以擦除结尾吗?
标准中说:
a.erase(q)
Requires: For vector and deque, T shall be
MoveAssignable.
Effects: Erases the element pointed to by q
尚不清楚 a.erase(a.end())
是连续容器的空操作还是 UB。想法?
来自 cplusplus.com:"An invalid position or range causes undefined behavior."
如果我们使用 this reference page for erase() :
The iterator pos must be valid and dereferenceable. Thus the end() iterator (which is valid, but is not dereferencable) cannot be used as a value for pos.
所以,不,它是无效的。
§23.2.3 [sequence.reqmts]/p3(强调我的):
In Tables 100 and 101, X
denotes a sequence container class, a
denotes a
value of X
containing elements of type T
, [...], q
denotes a valid dereferenceable const iterator to a
.
Table100是序列容器要求table包含a.erase(q)
.
也就是说,a.erase(a.end())
是UB。
顺序容器可以擦除结尾吗?
标准中说:
a.erase(q)
Requires: For vector and deque, T shall be
MoveAssignable.
Effects: Erases the element pointed to by q
尚不清楚 a.erase(a.end())
是连续容器的空操作还是 UB。想法?
来自 cplusplus.com:"An invalid position or range causes undefined behavior."
如果我们使用 this reference page for erase() :
The iterator pos must be valid and dereferenceable. Thus the end() iterator (which is valid, but is not dereferencable) cannot be used as a value for pos.
所以,不,它是无效的。
§23.2.3 [sequence.reqmts]/p3(强调我的):
In Tables 100 and 101,
X
denotes a sequence container class, a denotesa
value ofX
containing elements of typeT
, [...],q
denotes a valid dereferenceable const iterator toa
.
Table100是序列容器要求table包含a.erase(q)
.
也就是说,a.erase(a.end())
是UB。