什么应std :: vector :: data()返回如果向量是空的? [英] What should std::vector::data() return if the vector is empty?
问题描述
根据标准草案( 23.3.6.4矢量数据< a>),data()指向底层数组, [data(),data()+ size())
必须是有效的范围:
T * data()noexcept;
const T * data()const noexcept;
1返回:一个指针,使[data(),data()+ size())是一个有效的范围。对于非空向量,
data()==& front()。
2复杂性:恒定时间
但是如果向量是空的,当我构造一个零大小的向量:
#include< vector&
是(强调我):
#include< iostream>
int main(){
const int NUM = 0 * 10;
std :: vector< double> v(NUM,0.0);
std :: cerr<< V:< v.data()<< std :: endl; MSVC 2010返回null,但是在Linux上(使用GCC 4.2.1和英特尔®) 12.1)我得到一个非空地址。
是
vector :: data()
返回null?例如,一个实现是否可以执行默认大小的初始分配并返回一个(非空)指针?
编辑几个答案集中在空范围的有效性。我完全同意。
我真的想看到一个很好的参考或解释:是否允许,必须
解决方案范围的约定是
[inclusive,exclusive)
,也就是说,如果你遍历一个范围[X,Y)
-code):for(iterator ii = X; ii!= Y; ++ ii){
。 ..
}
这允许表示一个空的范围为
[X,X)
。此外,对于每个地址,此空白范围也是完美定义的,无论它是有效还是无效。
c $ c> data()
strong> 23.3.6.4 [vector.data]
const T * data()const noexcept;
返回: [data(),data()+ size())是有效范围的指针。对于
非空向量,data()==& front()。
唯一的无条件保证是
[data(),data()+ size())
应该是一个有效的范围。对于size()== 0
成员函数data()
可能返回任何值,空范围。因此,我会说如果size()
为零,则允许实现返回非空指针。According to the draft standard (23.3.6.4 vector data), data() points to the underlying array and
[data(), data() + size())
must be a valid range:T* data() noexcept; const T* data() const noexcept; 1 Returns: A pointer such that [data(),data() + size()) is a valid range. For a non-empty vector, data() == &front(). 2 Complexity: Constant time
But what if the vector is empty? When I construct a zero-size vector:
#include <vector> #include <iostream> int main() { const int NUM = 0*10; std::vector< double > v( NUM, 0.0 ); std::cerr << "V : "<< v.data() << std::endl; }
MSVC 2010 returns null, but on Linux (with GCC 4.2.1 and Intel 12.1) I get a non-null address.
Is
vector::data()
allowed to or should it return null? Could an implementation, for example, do a default-size initial allocation and return a (non-null) pointer to it?Edit: Several answers focus on the validity of an empty-range. I fully agree there.
I would really like to see a good reference or explanation for: Is it allowed to, must it return null or may it also return non-null?
解决方案The convention for ranges is
[inclusive, exclusive)
, that is if you iterate over a range[X,Y)
you will conceptually do the following (pseudo-code):for( iterator ii = X; ii != Y; ++ii) { ... }
This permits to express an empty range as
[X,X)
. Also this empty range is perfectly well defined for each address, no matter if it is valid or invalid.That said the requirements for
data()
are (emphasis mine):23.3.6.4 [vector.data]
T* data() noexcept;
const T* data() const noexcept;
Returns: A pointer such that [data(),data() + size()) is a valid range. For a non-empty vector, data() == &front().
It seems to me that the only unconditional guarantee is that
[data(),data() + size())
should be a valid range. Forsize() == 0
the member functiondata()
may return any value and the range will be a valid empty range. Therefore I would say that an implementation is allowed to return a non-null pointer ifsize()
is zero.这篇关于什么应std :: vector :: data()返回如果向量是空的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!