简体   繁体   English

我什么时候应该使用矢量 <int> :: size_type而不是size_t?

[英]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


  1. And for some specific 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. 对于某些特定的Tvector<T>::size_type也可能是一个不同的类型 - 您允许放入std命名空间的少数事项之一是用户定义类型的现有类的特化,因此对于某些Tvector<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_typevector<int>类型的(静态)成员类型。通常,它是std::size_ttypedef ,它本身通常是unsigned intunsigned long long的typedef。”

我认为他们是一样的。

typedef typename Allocator::size_type size_type;

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

相关问题 `size_t`总是`vector <int> :: size_type`或任何其他容器类型的别名? - Is `size_t` always an alias for `vector<int>::size_type` or any other container type? std :: size_t或std :: vector <Foo> ::尺码​​类型? - std::size_t or std::vector<Foo>::size_type? 为什么 std::array size_t 和 std::vector 中的 size_type 通常是 size_t? - Why is size_type in std::array size_t and in std::vector usually size_t? 可以在循环中使用int而不是size_t吗? - Is it okay to use int instead of a size_t in looping over a vector? 向量索引变量声明(size_t 或 std::vector<DATATYPE> ::尺码​​类型) - Vector index variable declaration (size_t or std::vector<DATATYPE>::size_type) 是否保证size_t,vector :: size_type等typedef不会绑定到char类型? - Is it guaranteed that size_t, vector::size_type, etc typedefs won't bind to a char type? 他为什么使用“typedef vector” <double> :: size_type“而不是使用”int“ - Why did he use “typedef vector<double>::size_type” instead of using “int” &#39;size_t&#39; 与 &#39;container::size_type&#39; - 'size_t' vs 'container::size_type' 保证std :: container :: size_type是std :: size_t - Guarantee that std::container::size_type is a std::size_t 正确使用向量 <int> ::尺码​​类型 - Correct usage of vector<int>::size_type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM