简体   繁体   English

向量索引变量声明(size_t 或 std::vector<DATATYPE> ::尺码​​类型)

[英]Vector index variable declaration (size_t or std::vector<DATATYPE>::size_type)

What is the best practice for declaring a new variable for comparison with the size of a vector?声明新变量以与向量大小进行比较的最佳做法是什么? which of the following should we favor (ie vector of doubles)?我们应该支持以下哪一项(即双打向量)?

  1. uint compareVar;
  2. std::uint64_t compareVar;
  3. std::size_t compareVar;
  4. std::vector<double>::size_type compareVar; // how is this different than size_t?

and why?为什么?

The one that you must use is std::vector::size_type .您必须使用的是std::vector::size_type it is usually std::size_t but standard doesn't specify it.它通常是std::size_t但标准没有指定它。 it is possible one implementation uses another type.一个实现可能使用另一种类型。 You should use it whenever you want to refer to std::vector size.每当您想引用std::vector大小时,都应该使用它。

uint , uint64_t and even size_t must not be used because in every implementation and platform the underlying vector implementation may use different types. uintuint64_t甚至size_t不能使用,因为在每个实现和平台中,底层向量实现可能使用不同的类型。

If you need an equality or ordinal comparison only, the best type would be the one used by vector's implementation, meaning如果您只需要相等或序数比较,最好的类型是 vector 的实现所使用的类型,这意味着

std::vector<MyType>::size_type compareVar

This guarantees that the type you use matches the type of vector's implementation regardless of the platform.这保证了无论平台如何,您使用的类型都与向量实现的类型相匹配。

Note that since the type is unsigned, computing the difference between vector's size and compareVar would require some caution to avoid incorrect results when subtracting a larger value from a smaller one.请注意,由于类型是无符号的,因此计算向量大小和compareVar之间的差异时需要谨慎,以避免从较小值中减去较大值时出现错误结果。

In order of "goodness": 4 3 1 2.按照“善良”的顺序:4 3 1 2。

std::vector::size_type is an implementation defined unsigned integer type that is guaranteed to be able to index all elements in any vector. std::vector::size_type是一个实现定义的无符号整数类型,保证能够索引任何向量中的所有元素。 It is good to use this, because it is guaranteed to be at least as large as is needed in all cases, but is not required to be any larger than that.最好使用它,因为它保证至少与所有情况下所需的一样大,但不需要比这更大。

std::size_t is an implementation defined unsigned integer type that is guaranteed to be able to represent the size of any object. std::size_t是一个实现定义的无符号整数类型,保证能够表示任何对象的大小。 std::vector::size_type is typically same as this, but that is not guaranteed in theory. std::vector::size_type通常与此相同,但这在理论上并不能保证。

uint There is no such type in C++, but I assume that you mean unsigned . uint C++ 中没有这样的类型,但我假设您的意思是unsigned This is not guaranteed to be able to represent all indices of a vector.这不能保证能够表示向量的所有索引。 In fact, on 64 bit systems which are ubiquitous, largest representable value of unsigned is often much much less than largest possible vector.事实上,在无处不在的 64 位系统上, unsigned最大可表示值通常远小于最大可能向量。 Still, if you know that the upper limit is much less in your program, then using this is probably fine... as long as that assumption never changes.尽管如此,如果您知道程序中的上限要小得多,那么使用它可能没问题……只要这个假设永远不会改变。

std::uint64_t is an unsigned type that is not guaranteed to exist on all systems, and is not guaranteed to be sufficiently large to represent all indexes of a vector, although it is likely be sufficient on 64 bit systems and those with even smaller address space, which essentially covers nearly all computers at the moment. std::uint64_t是一种无符号类型,不能保证在所有系统上都存在,也不能保证足够大以表示向量的所有索引,尽管它可能在 64 位系统和那些地址更小的系统上就足够了空间,它基本上涵盖了目前几乎所有的计算机。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM