是&(vector [0])总是有效的? [英] is &(vector[0]) always valid?

查看:72
本文介绍了是&(vector [0])总是有效的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的印象是以下内容始终有效:


std :: vector< Tv;

..

T * p =&(v [0]);


但我最近被告知,如果载体是<需要护理br />
为空,即当v.size()== 0.


我希望上面的表达式没问题,即使v为空但是

,如果你试图取消引用p,你就麻烦了。


如果以上都不行,我会的必须写:


T * p =(v.size()!= 0)? &(v [0]):0;


我查看了C ++ 98标准,但找不到任何东西

确认我的希望。我从表68中看到该表达式具有

操作语义。


&(*(v.begin()+ 0))


肯定*似乎*它可能反过来操作等效

到v.begin(),这确实看起来很安全,即使对于空向量也是如此。但是我现在感觉自己要处于非常薄的冰上。


所以,我可以写一下:


T * p =&(v [0]);


或者我必须写:


T * p =(v .size()!= 0)? &(v [0]):0;


谢谢,

John Salmon


---

[comp.std.c ++被审核。要提交文章,请尝试发布]

[您的新闻阅读器。如果失败,请使用mailto:st ***** @ ncar.ucar.edu]

[---请在发布前查看常见问题解答。 ---]

[常见问题: http:/ /www.comeaucomputing.com/csc/faq.html ]


I was under the impression that the following was always valid:

std::vector<Tv;
..
T *p = &(v[0]);

But I was recently told that care was needed in case the vector was
empty, i.e., when v.size() == 0.

I''m hoping that the above expression is fine, even if v is empty, but
that if you try to dereference p, you''re in trouble.

If the above isn''t ok, I''d have to write:

T *p = (v.size()!=0)? &(v[0]) : 0;

I took a look in the C++98 standard, and couldn''t find anything to
confirm my hope. I see from Table 68 that the expression has the
"operational sematics" of

&(*(v.begin() + 0))

which sure *seems* like it might in turn be "operationally equivalent"
to v.begin(), which sure seems safe, even for an empty vector. But I
now feel myself to be on very thin ice.

So, can I write:

T *p = &(v[0]);

or do I have to write:

T *p = (v.size()!=0)? &(v[0]) : 0;

Thanks,
John Salmon

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:st*****@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]

推荐答案

John Salmon写道:
John Salmon wrote:

我认为以下内容始终有效:


std :: vector< Tv;



T * p =&(v [0]);


但我最近被告知如果载体是<需要护理br />
为空,即当v.size()== 0.


我希望上面的表达式没问题,即使v为空,但是如果你试图取消引用p,你就会遇到麻烦。
I was under the impression that the following was always valid:

std::vector<Tv;
.
T *p = &(v[0]);

But I was recently told that care was needed in case the vector was
empty, i.e., when v.size() == 0.

I''m hoping that the above expression is fine, even if v is empty, but
that if you try to dereference p, you''re in trouble.



它可以在你的平台上运行,但是C ++标准说它是未定义的
。因此,今天它可能适合你,但明天也可能会破坏。

It may work on your platform, however the C++ standard says it''s
undefined. Hence, it may work for you today but it may also break tomorrow.


>

如果上述情况不好,我不得不写:


T * p =(v.size()!= 0)? &(v [0]):0;
>
If the above isn''t ok, I''d have to write:

T *p = (v.size()!=0)? &(v[0]) : 0;



正确。

Right.


John Salmon写道:
John Salmon wrote:

我的印象是以下内容始终有效:


std :: vector< Tv;



T * p =&(v [0]);


但我最近被告知,如果向量是空的,b / b是空的,需要小心,即,当v.size()== 0时。


我希望上面的表达式没问题,即使v为空,但

如果你试图取消引用p,你就麻烦了。


如果上述情况不好,我就得写:


T * p =(v.size()!= 0)? &(v [0]):0;


我查看了C ++ 98标准,但找不到任何东西

确认我的希望。我从表68中看到该表达式具有

操作语义。


&(*(v.begin()+ 0))


肯定*似乎*它可能反过来操作等效

到v.begin(),这确实看起来很安全,即使对于空向量也是如此。但是我现在感觉自己要处于非常薄的冰上。


所以,我可以写一下:


T * p =&(v [0]);


或者我必须写:


T * p =(v .size()!= 0)? &(v [0]):0;


谢谢,

John Salmon
I was under the impression that the following was always valid:

std::vector<Tv;
.
T *p = &(v[0]);

But I was recently told that care was needed in case the vector was
empty, i.e., when v.size() == 0.

I''m hoping that the above expression is fine, even if v is empty, but
that if you try to dereference p, you''re in trouble.

If the above isn''t ok, I''d have to write:

T *p = (v.size()!=0)? &(v[0]) : 0;

I took a look in the C++98 standard, and couldn''t find anything to
confirm my hope. I see from Table 68 that the expression has the
"operational sematics" of

&(*(v.begin() + 0))

which sure *seems* like it might in turn be "operationally equivalent"
to v.begin(), which sure seems safe, even for an empty vector. But I
now feel myself to be on very thin ice.

So, can I write:

T *p = &(v[0]);

or do I have to write:

T *p = (v.size()!=0)? &(v[0]) : 0;

Thanks,
John Salmon



v [0]对空向量无效,因此& v [0]无效

。你对操作语义是正确的,但是表达式有效也不是
。如果向量为空,则begin()== end(),

,因此您取消引用结束()迭代器。


至于你提出的替代表达式,使用empty()而不是

size()会更加惯用,并且可能更高效,取决于实施的



T * p = v.empty()? 0:&(v [0]);


-

Alan Johnson


---

[comp.std.c ++被审核。要提交文章,请尝试发布]

[您的新闻阅读器。如果失败,请使用mailto:st ***** @ ncar.ucar.edu]

[---请在发布前查看常见问题解答。 ---]

[常见问题: http:/ /www.comeaucomputing.com/csc/faq.html ]

v[0] is NOT valid for an empty vector, and therefore &v[0] is not valid
either. You are right about the operational semantics, but neither is
that expression valid. If the vector is empty, then begin() == end(),
and you are therefore "dereferencing" the end() iterator.

As for the alternate expression you propose, using empty() rather than
size() would be more idiomatic, and might be more efficient, depending
on the implementation:

T *p = v.empty() ? 0 : &(v[0]) ;

--
Alan Johnson

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:st*****@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]


文章< m3 ********* ***@river.fishnet>,John Salmon

< js ***** @ thesalmons.orgwrote:
In article <m3************@river.fishnet>, John Salmon
<js*****@thesalmons.orgwrote:

我是在下面总是有效的印象中:


std :: vector< Tv;



T * p = &(v [0]);


但我最近被告知,如果向量是空的,即在v时,需要小心。 size()== 0.
I was under the impression that the following was always valid:

std::vector<Tv;
.
T *p = &(v[0]);

But I was recently told that care was needed in case the vector was
empty, i.e., when v.size() == 0.



如果v.size()== 0则无效。

template< class T, A类>

内联T *数据(std :: vector< T,A& v)

{

返回v.empty() ? 0:& v [0];

}

是一个简单的解决方法。最近的标准草案提供了带有data()成员函数的
向量,但是你的

实现可能没有。


---

[comp.std.c ++经过审核。要提交文章,请尝试发布]

[您的新闻阅读器。如果失败,请使用mailto:st ***** @ ncar.ucar.edu]

[---请在发布前查看常见问题解答。 ---]

[常见问题: http:/ /www.comeaucomputing.com/csc/faq.html ]

its not valid if v.size()==0.
template <class T,class A>
inline T *data(std::vector<T,A&v)
{
return v.empty() ? 0: &v[0];
}
is an easy work around. Recent drafts of the standard provide
vector with a data() member function which does this, but your
implementation probably does not.

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:st*****@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]


这篇关于是&amp;(vector [0])总是有效的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆