‘size_t’vs‘容器::size_type

时间:2022-03-31 17:01:05

Is there is a difference between size_t and container::size_type?

size_t和container: size_type有什么不同吗?

What I understand is size_t is more generic and can be used for any size_types.

我所理解的是size_t更加通用,可以用于任何size_type。

But is container::size_type optimized for specific kinds of containers?

但是容器:size_type是否针对特定类型的容器进行了优化?

3 个解决方案

#1


88  

The standard containers define size_type as a typedef to Allocator::size_type (Allocator is a template parameter), which for std::allocator<T>::size_type is typically defined to be size_t (or a compatible type). So for the standard case, they are the same.

标准容器将size_type定义为从类型定义到分配器::size_type(分配器是一个模板参数),对于std:::分配器 : size_type通常定义为size_t(或兼容类型)。对于标准情况,它们是一样的。

However, if you use a custom allocator a different underlying type could be used. So container::size_type is preferable for maximum generality.

但是,如果使用自定义分配器,则可以使用不同的底层类型。所以容器::size_type是最通用的。

#2


32  

  • size_t is defined as the type used for the size of an object and is platform dependent.
  • size_t定义为用于对象大小的类型,并且与平台相关。
  • container::size_type is the type that is used for the number of elements in the container and is container dependent.
  • 容器::size_type是用于容器中元素数量的类型,并且与容器相关。

All std containers use size_t as the size_type, but each independent library vendor chooses a type that it finds appropriate for its container.

所有std容器都使用size_t作为size_type,但是每个独立的库供应商都选择适合其容器的类型。

If you look at , you'll find that the size_type of Qt containers is version dependent. In Qt3 it was unsigned int and in Qt4 it was changed to int.

如果您查看qt,您会发现qt容器的size_type是依赖于版本的。在Qt3中,它是无符号int,在Qt4中,它被更改为int。

#3


8  

For std::[w]string, std::[w]string::size_type is equal to std::allocator<T>::size_type, which is equal to the std::size_t. For other containers, it's some implementation defined unsigned integer type.

对于std::[w]string, std::[w]string:::size_type等于std:::allocator : size_type,等于std: size_t。对于其他容器,它是一些定义为无符号整数类型的实现。

Sometimes it's useful to have the exact type, so for example one knows where the type wraps around to (like, to UINT_MAX) so that one can make use of that. Or for templates, where you really need to pass two identical types to function/class templates.

有时候,拥有确切的类型是很有用的,例如,我们知道类型在哪里(比如UINT_MAX),这样我们就可以利用它。或者对于模板,您需要向函数/类模板传递两个相同的类型。

Often i find i use size_t for brevity or iterators anyway. In generic code, since you generally don't know with what container instance your template is used and what size those containers have, you will have to use the Container::size_type typedef if you need to store the containers size.

我经常发现我使用size_t作为简洁或迭代器。在通用代码中,由于您通常不知道使用的是什么容器实例,以及这些容器的大小,所以如果需要存储容器的大小,您必须使用容器::size_type typedef。

#1


88  

The standard containers define size_type as a typedef to Allocator::size_type (Allocator is a template parameter), which for std::allocator<T>::size_type is typically defined to be size_t (or a compatible type). So for the standard case, they are the same.

标准容器将size_type定义为从类型定义到分配器::size_type(分配器是一个模板参数),对于std:::分配器 : size_type通常定义为size_t(或兼容类型)。对于标准情况,它们是一样的。

However, if you use a custom allocator a different underlying type could be used. So container::size_type is preferable for maximum generality.

但是,如果使用自定义分配器,则可以使用不同的底层类型。所以容器::size_type是最通用的。

#2


32  

  • size_t is defined as the type used for the size of an object and is platform dependent.
  • size_t定义为用于对象大小的类型,并且与平台相关。
  • container::size_type is the type that is used for the number of elements in the container and is container dependent.
  • 容器::size_type是用于容器中元素数量的类型,并且与容器相关。

All std containers use size_t as the size_type, but each independent library vendor chooses a type that it finds appropriate for its container.

所有std容器都使用size_t作为size_type,但是每个独立的库供应商都选择适合其容器的类型。

If you look at , you'll find that the size_type of Qt containers is version dependent. In Qt3 it was unsigned int and in Qt4 it was changed to int.

如果您查看qt,您会发现qt容器的size_type是依赖于版本的。在Qt3中,它是无符号int,在Qt4中,它被更改为int。

#3


8  

For std::[w]string, std::[w]string::size_type is equal to std::allocator<T>::size_type, which is equal to the std::size_t. For other containers, it's some implementation defined unsigned integer type.

对于std::[w]string, std::[w]string:::size_type等于std:::allocator : size_type,等于std: size_t。对于其他容器,它是一些定义为无符号整数类型的实现。

Sometimes it's useful to have the exact type, so for example one knows where the type wraps around to (like, to UINT_MAX) so that one can make use of that. Or for templates, where you really need to pass two identical types to function/class templates.

有时候,拥有确切的类型是很有用的,例如,我们知道类型在哪里(比如UINT_MAX),这样我们就可以利用它。或者对于模板,您需要向函数/类模板传递两个相同的类型。

Often i find i use size_t for brevity or iterators anyway. In generic code, since you generally don't know with what container instance your template is used and what size those containers have, you will have to use the Container::size_type typedef if you need to store the containers size.

我经常发现我使用size_t作为简洁或迭代器。在通用代码中,由于您通常不知道使用的是什么容器实例,以及这些容器的大小,所以如果需要存储容器的大小,您必须使用容器::size_type typedef。