C++ begin() == end() 是否适用于任何 empty() 向量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17796200/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is begin() == end() for any empty() vector?
提问by OldPeculier
I have long assumed that for any empty std::vector
V, V.begin() == V.end()
. Yet I see nothing in the C++ specification that states this to alwaysbe true. Is it necessarily true or does it just happen to be true on most implementations?
我长期以来一直假设对于任何空的std::vector
V, V.begin() == V.end()
. 然而,我在 C++ 规范中没有看到任何声明这总是正确的。它一定是真的还是碰巧在大多数实现中都是真的?
回答by Rapptz
Yes, that's what the standard requires it to be for empty()
for any container.
是的,这就是标准empty()
对任何容器的要求。
§ 23.2.1 Table 96 of the C++11 standard says:
C++11 标准的第 23.2.1 节表 96 说:
+----------+---------------+----------------------+
|Expression| Return Type | Operational Semantics|
|----------|---------------|----------------------|
|a.empty() |Convertible |a.begin() == a.end() |
| |to bool | |
| | | |
+-------------------------------------------------+
回答by juanchopanza
23.2.1 General container requirements, specifically Table 96 Container Requirementshas
23.2.1 一般集装箱要求,特别是表 96集装箱要求有
a.empty()
convertible tobool
, operational semanticsa.begin() == a.end()
a.empty()
可转换为bool
, 操作语义a.begin() == a.end()
Then
然后
6
begin()
returns an iterator referring to the first element in the container.end()
returns an iterator which is the past-the-end value for the container. If the container is empty, thenbegin() == end();
6
begin()
返回一个引用容器中第一个元素的迭代器。end()
返回一个迭代器,它是容器的最后值。如果容器是空的,那么begin() == end();
(emphasis mine)
(强调我的)
回答by Jiminion
http://www.cplusplus.com/reference/vector/vector/end/
http://www.cplusplus.com/reference/vector/vector/end/
If the container is empty, end() is the same as begin().
如果容器为空,则 end() 与 begin() 相同。