C++ 向量大小类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7636323/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 17:15:54  来源:igfitidea点击:

C++ vector size types

c++vectorint

提问by Kerrek SB

I just started learning C++ and have a question about vectors. The book I'm reading states that if I want to extract the size of a vector of type double (for example), I should do something like:

我刚开始学习 C++ 并且有一个关于向量的问题。我正在阅读的书指出,如果我想提取 double 类型的向量的大小(例如),我应该执行以下操作:

vector<double>::size_type vector_size;
vector_size = myVector.size();

Whereas in Java I might do

而在 Java 中我可能会这样做

int vector_size;
vector_size = myVector.size();

My question is, why is there a type named vector::size_type? Why doesn't C++ just use int?

我的问题是,为什么有一个名为 vector::size_type 的类型?为什么 C++ 不只使用 int?

回答by Kerrek SB

C++ is a language for library writing*, and allowing the author to be as general as possible is one of its key strengths. Rather than prescribing the standard containers to use any particular data type, the more general approach is to decree that each container expose a size_typemember type. This allows for greater flexibility and genericity. For example, consider this generic code:

C++ 是一种用于库编写的语言*,允许作者尽可能通用是其主要优势之一。不是规定标准容器使用任何特定的数据类型,更通用的方法是规定每个容器公开一个size_type成员类型。这允许更大的灵活性和通用性。例如,考虑这个通用代码:

template <template <typename...> Container, typename T>
void doStuff(const Container<T> & c)
{
  typename Container<T>::size_type n = c.size();
  // ...
}

This code will work on anycontainer template (that can be instantiated with a single argument), and we don't impose any unnecessary restrictions on the user of our code.

此代码适用于任何容器模板(可以使用单个参数实例化),并且我们不会对代码的用户施加任何不必要的限制。

(In practice, most size types will resolve to std::size_t, which in turn is an unsigned type, usually unsigned intor unsigned long-- but why should we have to know that?)

(在实践中,大多数大小类型将解析为std::size_t,而后者又是无符号类型,通常unsigned intunsigned long——但为什么我们必须知道这一点?)

*) I'm not sure what the corresponding statement for Java would be.

*) 我不确定 Java 的相应语句是什么。

回答by GManNickG

Java does not have unsigned integer types, so they have to go with int.

Java 没有无符号整数类型,因此它们必须与int.

Contrarily, C++ does and uses them where appropriate (where negative values are nonsensical), the canonical example being the length of something like an array.

相反,C++ 会在适当的地方使用它们(负值是无意义的),典型的例子是数组的长度。

回答by Wyzard

The C++ standard says that a container's size_typeis an unsigned integral type, but it doesn't specify which one; one implementation might use unsigned intand another might use unsigned long, for example.

C++ 标准说容器size_type是无符号整数类型,但它没有指定是哪一个;例如,一个实现可能使用unsigned int,另一个可能使用unsigned long

C++ isn't "shielded" from platform-specific implementation details as much as Java is. The size_typealias helps to shield yourcode from such details, so it'll work properly regardless of what actual type should be used to represent a vector's size.

C++ 不像 Java 那样“屏蔽”特定于平台的实现细节。该size_type别名有助于保护从这些细节的代码,所以它会正常工作,无论什么实际类型应该被用来表示一个矢量的大小。

回答by Cheers and hth. - Alf

The book you’re reading states that if you want to extract the size of a vector of type double (for example), you should do something like:

您正在阅读的这本书指出,如果您想提取 double 类型的向量的大小(例如),您应该执行以下操作:

    vector<double>::size_type vector_size;
    vector_size = myVector.size();

Whereas in Java you might do

而在 Java 中你可能会这样做

    int vector_size;
    vector_size = myVector.size();

Both are inferior options in C++. The first is extremely verbose and unsafe (mostly due to implicit promotions). The second is verbose and extremely unsafe (due to number range).

两者都是 C++ 中的劣等选项。第一个非常冗长且不安全(主要是由于隐式促销)。第二个是冗长且极其不安全的(由于数字范围)。

In C++, do

在 C++ 中,做

    ptrdiff_t const vectorSize = myVector.size();

Note that

注意

  • ptrdiff_t, from the stddef.hheader, is a signed type that is guaranteed large enough.

  • Initialization is done in the declaration (this is better C++ style).

  • The samenaming convention has been applied to both variables.

  • ptrdiff_t,从头开始stddef.h,是保证足够大的有符号类型。

  • 初始化在声明中完成(这是更好的 C++ 风格)。

  • 相同的命名约定已经被应用到两个变量。

In summary, doing the right thing is shorter and safer.

总之,做正确的事情更短更安全。

Cheers & hth.,

干杯 & hth.,

回答by ereOn

My personal feeling about this is that it is for a better code safety/readability.

我个人对此的感觉是为了更好的代码安全性/可读性。

For me intis a type which conveys no special meaning: it can number apples, bananas, or anything.

对我来说int是一种没有任何特殊意义的类型:它可以给苹果、香蕉或任何东西编号。

size_type, which is probably a typedeffor size_thas a stronger meaning: it indicates a size, in bytes.

size_type, 这可能是一个typedefforsize_t有更强烈的含义:它表示一个大小,以字节为单位。

That is, it is easier to know what a variable mean. Of course, following this rationale, there could be a lot of different types for different units. But a "buffer size" is really a common case so it somehow deserves a dedicated type.

也就是说,更容易知道变量的含义。当然,按照这个原理,不同的单位可能会有很多不同的类型。但是“缓冲区大小”确实是一种常见情况,因此它以某种方式值得一个专用类型。

Another aspect is code maintability: if the container suddenly changes its size_typefrom say, uint64_tto unsigned intfor instance, using size_typeyou don't have to change it in every source code relying on it.

另一个方面是代码maintability:如果容器突然改变其size_type从说,uint64_tunsigned int例如,使用size_type你不必去改变它在每一个源代码依赖于它。