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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 17:56:35  来源:igfitidea点击:

'size_t' vs 'container::size_type'

c++size-typecontainer-data-type

提问by Charles Khunt

Is there is a difference between size_tand container::size_type?

是否有之间的差异size_tcontainer::size_type

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

我的理解是size_t更通用,可用于任何size_types。

But is container::size_typeoptimized for specific kinds of containers?

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

采纳答案by Evan Teran

The standard containers define size_typeas a typedef to Allocator::size_type(Allocator is a template parameter), which for std::allocator<T>::size_typeis 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_typeis preferable for maximum generality.

但是,如果您使用自定义分配器,则可以使用不同的基础类型。所以container::size_type最好是为了最大的通用性。

回答by TimW

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

All stdcontainers use size_tas 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_typeof Qt containers is version dependent. In Qt3 it was unsigned intand in Qt4 it was changed to int.

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

回答by Johannes Schaub - litb

For std::[w]string, std::[w]string::size_typeis 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_tfor 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_typetypedef if you need to store the containers size.

我经常发现我size_t为了简洁或迭代器而使用它。在通用代码中,由于您通常不知道模板使用的是哪个容器实例以及这些容器的大小,因此Container::size_type如果需要存储容器大小,则必须使用typedef。