[英]When should I use vector<int>::size_type instead of size_t?
In this question I see following: 在这个问题中我看到以下内容:
for (vector<int>::size_type ix = 0; ix ! = ivec.size(); ++ix) {
ivec[ix] = 0;
}
I understand that why int
is not used here, but why not just use size_t
? 我明白为什么不使用
int
,但为什么不使用size_t
?
Under what circumstances I should use vector<int>::size_type
instead of size_t
? 在什么情况下我应该使用
vector<int>::size_type
而不是size_t
?
The primary time to use size_type
is in a template. 使用
size_type
的主要时间是在模板中。 Although std::vector<T>::size_type
is usually size_t
, some_other_container<T>::size_type
might be some other type instead 1 . 虽然
std::vector<T>::size_type
通常是size_t
,但some_other_container<T>::size_type
可能是其他类型而不是1 。 One of the few things a user is allowed to add to the std
namespace is a specialization of an existing template for some user defined type. 允许用户添加到
std
命名空间的少数几件事之一是针对某些用户定义类型的现有模板的特化。 Therefore, std::vector<T>::size_type
for some oddball T
could actually be some type other than size_t
, even though the base template defined in the standard library probably always uses size_t
. 因此,某些古怪
T
std::vector<T>::size_type
实际上可能是size_t
以外的某种类型,即使标准库中定义的基本模板可能总是使用size_t
。
Therefore, if you want to use the correct type for a specific container inside a template that works with that container, you want to use container::size_type
instead of just assuming size_t
. 因此,如果要对使用该容器的模板内的特定容器使用正确的类型,则需要使用
container::size_type
而不是假设size_t
。
Note, however, that generic code should rarely work directly with a container. 但请注意,通用代码很少直接与容器一起使用。 Instead, it should typically work with iterators, so instead of
container<T>::size_type
, it would typically use something like std::iterator_traits<WhateverIterator>::difference_type
instead. 相反,它通常应该使用迭代器,因此它代替
container<T>::size_type
,它通常使用类似std::iterator_traits<WhateverIterator>::difference_type
。
T
, vector<T>::size_type
might be a different type as well--one of the few things you're allowed to put into the std
namespace is a specialization of an existing class for a user-defined type, so for some T
, vector<T>
could use a completely different container than for most other types. T
, vector<T>::size_type
也可能是一个不同的类型 - 您允许放入std
命名空间的少数事项之一是用户定义类型的现有类的特化,因此对于某些T
, vector<T>
可以使用与大多数其他类型完全不同的容器。 This is typical for vector<bool>
, but possible for other types as well. vector<bool>
是典型的,但对于其他类型也是如此。 One reason to use it is consistency. 使用它的一个原因是一致性。 While it is true that
size_t
is sufficient to index/count a std::vector
, it conceptually insufficient to index/count a std::list
or any other non-array based container. 虽然
size_t
确实足以索引/计数std::vector
,但它在概念上不足以索引/计数std::list
或任何其他非基于数组的容器。 So, when working with containers, you should typically use container_type::size_type
. 因此,在使用容器时,通常应使用
container_type::size_type
。
In generic code, when the actual type of the container is not known, you have no choice but to use container_type::size_type
. 在通用代码中,当容器的实际类型未知时,您别无选择,只能使用
container_type::size_type
。 And even in specific code, when the container is known to be a std::vector
, there's no need to make an exception and suddenly switch to size_t
. 甚至在特定代码中,当已知容器是
std::vector
,不需要发出异常并突然切换到size_t
。
From: vector<int>::size_type in C++ 来自: C ++中的vector <int> :: size_type
" size_type
is a (static) member type of the type vector<int>
. Usually, it is a typedef
for std::size_t
, which itself is usually a typedef for unsigned int
or unsigned long long
." “
size_type
是vector<int>
类型的(静态)成员类型。通常,它是std::size_t
的typedef
,它本身通常是unsigned int
或unsigned long long
的typedef。”
我认为他们是一样的。
typedef typename Allocator::size_type size_type;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.