问题描述
std :: vector< T> :: size_type保证与
std :: vector< U> :: size_type?
更明确,给定
void f(T,U);
和
std :: vector< Tvt;
std :: vector< Uvu;
大小相同,我可以写(
for(std: :vector< T> :: size_type i = 0; i< vt.size(); ++ i)f(vt [i],
vu [i]);
或者我应该写(
for(std :: vector< T> :: size_type i = 0; i< vt.size(); + + i)f(vt [i],
vu [static_cast< std :: vector< U> :: size_type>(i)]);
谢谢。
Is std::vector<T>::size_type guaranteed to be the same type as
std::vector<U>::size_type?
To be more explicit, given
void f(T, U);
and
std::vector<Tvt;
std::vector<Uvu;
which have the same size, can I write
for (std::vector<T>::size_type i = 0; i < vt.size(); ++i) f(vt[i],
vu[i]);
or should I write
for (std::vector<T>::size_type i = 0; i < vt.size(); ++i) f(vt[i],
vu[static_cast<std::vector<U>::size_type>(i)]);
Thank you.
推荐答案
我在标准中找不到任何此类保证,但如果它们有所不同,我会非常* *
感到惊讶。首先,没有理由让它们变得与众不同,其次,很难让它们与T和
U不同,而不会让它们依赖于T和U.方式(对于类型T和U的假设,这将是
)。大多数实现
可能使用size_t作为std :: vector< T> :: size_type。
-
Erik Wikstr? ?m
I can not find any such guarantee in the standard, but I would be *very*
surprised if they were of different. First, there is no reason to make
them different and second, it is difficult to make them differ for T and
U without having them depend on T and U in some way (which would be the
same as making assumptions about the types T and U). Most implementation
probably uses size_t as std::vector<T>::size_type.
--
Erik Wikstr??m
编号(但实际上,它们都与std :: size_t相同。)
No. (But in practice, they will all be the same as std::size_t.)
是。
Yes.
No.
更明确:因为你保证两个向量具有相同的
长度,std :: vector< T> :: size_type和std :: vector< U> :: size_type很大
足以代表它。此外,两种类型都保证是无符号的
整数类型。因此,作业定义明确。
最佳
Kai-Uwe Bux
No.
To be more explicit: since you promise that the two vectors have the same
length, std::vector<T>::size_type and std::vector<U>::size_type are large
enough to represent it. Also, both types are guaranteed to be unsigned
integral types. Therefore, the assignment is well-defined.
Best
Kai-Uwe Bux
可以安全地假设size_t具有相同的大小并且
类型为std :: vector< T> :: size_type?
前者写起来容易得多,这就是为什么
我很想用它而不是后者。
顺便说一下,我注意到gcc和VC ++都没有要求你把它写成std :: size_t,而只是size_t,而只是size_t。没问题
(没有''使用'')。这也是标准吗?
Can size_t be safely assumed to be of the same size and
type as std::vector<T>::size_type?
The former is just a lot easier to write, which is why
I''m tempted to use it instead of the latter.
Btw, I have noticed that neither gcc nor VC++ require you
to write it like "std::size_t", but just "size_t" is ok
(without any ''using''). Is this also standard?
这篇关于矢量:: SIZE_TYPE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!