C++矢量大小类型
我刚刚开始学习 C++,有一个关于向量的问题。 我正在读的书指出,如果我想提取 double 类型的向量的大小(例如),我应该这样做:
vector<double>::size_type vector_size;
vector_size = myVector.size();
而在 Java 中我可能会这样做
int vector_size;
vector_size = myVector.size();
我的问题是,为什么有一个名为向量的类型: :尺寸类型?为什么C++不直接使用int呢?
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:
vector<double>::size_type vector_size;
vector_size = myVector.size();
Whereas in Java I might do
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
C++ 标准规定容器的
size_type
是无符号整型,但没有指定是哪一种;例如,一种实现可能使用unsigned int
,另一种实现可能使用unsigned long
。C++ 不像 Java 那样“屏蔽”特定于平台的实现细节。
size_type
别名有助于保护您的代码免受此类细节的影响,因此无论应使用什么实际类型来表示向量的大小,它都会正常工作。The C++ standard says that a container's
size_type
is an unsigned integral type, but it doesn't specify which one; one implementation might useunsigned int
and another might useunsigned long
, for example.C++ isn't "shielded" from platform-specific implementation details as much as Java is. The
size_type
alias helps to shield your code from such details, so it'll work properly regardless of what actual type should be used to represent a vector's size.我个人的感觉是,这是为了更好的代码安全性/可读性。
对我来说,int 是一种没有特殊含义的类型:它可以对苹果、香蕉或任何东西进行编号。
size_type
,可能是size_t
的typedef
,具有更强的含义:它指示大小(以字节为单位)。也就是说,更容易知道变量的含义。当然,根据这个原理,不同的单位可能有很多不同的类型。但“缓冲区大小”确实是一种常见情况,因此它在某种程度上值得一个专用类型。
另一个方面是代码可维护性:如果容器突然将其
size_type
从uint64_t
更改为unsigned int
,例如使用size_type< /code> 您不必在依赖它的每个源代码中更改它。
My personal feeling about this is that it is for a better code safety/readability.
For me
int
is a type which conveys no special meaning: it can number apples, bananas, or anything.size_type
, which is probably atypedef
forsize_t
has a stronger meaning: it indicates a size, in bytes.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_type
from say,uint64_t
tounsigned int
for instance, usingsize_type
you don't have to change it in every source code relying on it.您正在阅读的书指出,如果您想提取 double 类型的向量的大小(例如),您应该执行以下操作:
而在 Java 中您可能会这样做
在 C++ 中两者都是较差的选项。第一个非常冗长且不安全(主要是由于隐式升级)。第二个是冗长且极其不安全的(由于数字范围)。
在 C++ 中,请
注意,
stddef.h
标头的ptrdiff_t
是保证足够大的有符号类型。初始化在声明中完成(这是更好的 C++ 风格)。
两个变量都应用了相同命名约定。
总而言之,做正确的事情更短、更安全。
干杯&呵呵,,
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:
Whereas in Java you might do
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).
In C++, do
Note that
ptrdiff_t
, from thestddef.h
header, is a signed type that is guaranteed large enough.Initialization is done in the declaration (this is better C++ style).
The same naming convention has been applied to both variables.
In summary, doing the right thing is shorter and safer.
Cheers & hth.,
C++ 是一种用于库编写的语言*,允许作者尽可能通用是其关键优势之一。更通用的方法不是规定标准容器使用任何特定的数据类型,而是规定每个容器公开一个
size_type
成员类型。这允许更大的灵活性和通用性。例如,考虑以下通用代码:此代码适用于任何容器模板(可以使用单个参数实例化),并且我们不会对代码的用户施加任何不必要的限制。
(实际上,大多数大小类型将解析为
std::size_t
,而后者又是无符号类型,通常为unsigned int
或unsigned long
-- 但为什么我们必须知道这一点?)*)我不确定 Java 的相应语句是什么。
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_type
member type. This allows for greater flexibility and genericity. For example, consider this generic code:This code will work on any container 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, usuallyunsigned int
orunsigned long
-- but why should we have to know that?)*) I'm not sure what the corresponding statement for Java would be.
Java 没有无符号整数类型,因此必须使用
int
。相反,C++ 在适当的地方使用它们(负值是无意义的),典型的例子是数组之类的东西的长度。
Java does not have unsigned integer types, so they have to go with
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.