C++ 'size_t' 与 'container::size_type'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/918567/
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
'size_t' vs 'container::size_type'
提问by Charles Khunt
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_type
s.
我的理解是size_t
更通用,可用于任何size_type
s。
But is container::size_type
optimized for specific kinds of containers?
但是是否container::size_type
针对特定类型的容器进行了优化?
采纳答案by Evan Teran
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 typicallydefined to be size_t
(or a compatible type). So for the standard case, they are the same.
标准容器限定size_type
为一个typedef到Allocator::size_type
(分配器是模板参数),其为std::allocator<T>::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.
但是,如果您使用自定义分配器,则可以使用不同的基础类型。所以container::size_type
最好是为了最大的通用性。
回答by TimW
size_t
is defined as the type used for the size of an object and is platform dependent.container::size_type
is the type that is used for the number of elements in the container and is container dependent.
size_t
被定义为用于对象大小的类型并且是平台相关的。container::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 qt, 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,您会发现size_type
Qt 容器的版本依赖于版本。在 Qt3 中它是unsigned int
,在 Qt4 中它被更改为int
.
回答by Johannes Schaub - litb
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<T>::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.
有时拥有确切的类型很有用,例如,我们知道类型在何处环绕(如 to 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
为了简洁或迭代器而使用它。在通用代码中,由于您通常不知道模板使用的是哪个容器实例以及这些容器的大小,因此Container::size_type
如果需要存储容器大小,则必须使用typedef。