size_t和size_type [英] size_t and size_type

查看:96
本文介绍了size_t和size_type的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

size_t和vector< T> :: size_type有什么区别?


它们是不同的类型还是不同的尺寸?


当前者较短时,为什么要键入后者?


其他STL类中的size_type类型怎么样?


- -

问候,抱怨

解决方案

2004年4月1日星期四16:17:54 +0200 ,Grumble< in ***** @ kma.eu.org>写道:

size_t和vector< T> :: size_type有什么区别?


这应该在FAQ中。


底线:/可能/无。标准明确允许容器

实现者假设他们交给的分配器(从中可以将

size_type typedef传播到容器的客户端,

虽然我的库实现只是硬连接size_t用于

容器typedef)但是将size_type定义为size_t。扩展也是允许的(见20.1.5 / 4)。

它们是不同的类型或不同的大小?


如果有任何此类实施,我还不知道它们。

当前者较短时,为什么要输入后者?


他们要么想到某些情况下集装箱的内存

来自某些堆,内存管理规则完全不同

需要它自己独特的size_type,否则这只是英特尔分段架构内存模型的工件,它影响了第一个分配器的创建

地点。谁知道......

其他STL课程的size_type类型怎么样?




同样的交易,AFAIK。

-leor

-

Leor Zolman --- BD软件--- www.bdsoft.com

C / C ++,Java,Perl和Unix的现场培训

C ++用户:下载BD Software的免费STL错误消息解密器:
www .bdsoft.com / tools / stlfilt.html


Grumble写道:


size_t有什么区别和矢量< T> :: size_type?

它们是不同的类型还是不同的尺寸?

当前者更短时,为什么要输入后者呢? />
其他STL类中的size_type类型怎么样?

问候,Grumble



vector< T>: :size_type i实现定义。对于

其他标准容器也是如此。一般来说,你不能依赖它是size_t。

我已经看到它被定义(最常见)为

typedef size_t size_type;


以及

typedef _Allocator :: size_type size_type;


其中_Allocator是容器的分配器模板参数。

在后一种情况下,你实际上可能最终得到的定义不同于

size_t(虽然标准分配器将size_type定义为size_t,但没有这样的

要求对于自定义分配器)。


我认为你最好使用通用定义(例如Container :: size_type)

通用代码,它会使它成为现实代码更通用(抱歉措辞)。


Denis




Denis Remezov <德********************* @ yahoo.ca.removethis>写在

消息新闻:40 *************** @ yahoo.ca.removethis ...

Grumble写道:< blockquote class =post_quotes>
size_t和vector< T> :: size_type有什么区别?

它们是不同的类型还是不同的大小?
<当前者更短时,为什么要输入后者?

其他STL类中的size_type类型怎么样?

-
问候,抱怨



vector< T> :: size_type是实现定义的。对于
其他标准容器也是如此。一般来说,你不能依赖它size_t。




实际上没有,vector< T> :: size_type使用标准分配器,所以它的

size_type必须是size_t。


但是当我们考虑自定义分配器时,事情变得有趣。然后你就是

,你不能假设向量< T,some_allocator> :: size_type是

size_t。但是,C ++标准明确允许标准库的

实现的权限,以假设向量< T,

some_allocator> :: size_type是size_t(所有其他的相同)

容器)。


换句话说,你不能将分配器传递给标准库容器
使用size_type不同的
类型来自size_t并期望它能够正常工作。


我想象标准库

实现者的这个奇怪的脱离条款是这样的,现有的实现没有失效(但

只是猜测。


john


What is the difference between size_t and vector<T>::size_type?

Are they ever a different type or a different size?

Why should one type the latter when the former is shorter?

What about the size_type type in other STL classes?

--
Regards, Grumble

解决方案

On Thu, 01 Apr 2004 16:17:54 +0200, Grumble <in*****@kma.eu.org> wrote:

What is the difference between size_t and vector<T>::size_type?
This should really be in the FAQ.

Bottom line: /probably/ none. The Standard explicitly allows container
implementors to assume that the allocators they''re handed (from which the
size_type typedef may be propagated to the clients of the container,
although I have library implementations that just hard-wire size_t for the
container typedefs) define size_type as size_t. Extensions are also
permitted (see 20.1.5/4).

Are they ever a different type or a different size?
If there are any such implementations, I don''t know about them yet.

Why should one type the latter when the former is shorter?
Either they were thinking ahead to some scenario when containers'' memory
would come from some heap with totally different rules of memory management
that needed its own unique size_type, or else this is just an artifact of
the Intel segmented architecture memory-model that influenced the creation
of allocators in the first place. Who knows...

What about the size_type type in other STL classes?



Same deal, AFAIK.
-leor
--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: Download BD Software''s free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html


Grumble wrote:


What is the difference between size_t and vector<T>::size_type?

Are they ever a different type or a different size?

Why should one type the latter when the former is shorter?

What about the size_type type in other STL classes?

--
Regards, Grumble


vector<T>::size_type is implementation-defined. The same holds for the
other standard containers. Generally, you cannot rely on it being size_t.
I''ve seen it defined (most commonly) as
typedef size_t size_type;

as well as
typedef _Allocator::size_type size_type;

where _Allocator was the container''s allocator template parameter.
In the latter case you could actually end up getting it defined differently from
size_t (though the standard allocator defines size_type as size_t, there is no such
requirement for custom allocators).

I think you are better off using generic definitions (such as Container::size_type)
in generic code anyway, it makes it more generic (sorry for the wording).

Denis



"Denis Remezov" <de*********************@yahoo.ca.removethis> wrote in
message news:40***************@yahoo.ca.removethis...

Grumble wrote:


What is the difference between size_t and vector<T>::size_type?

Are they ever a different type or a different size?

Why should one type the latter when the former is shorter?

What about the size_type type in other STL classes?

--
Regards, Grumble


vector<T>::size_type is implementation-defined. The same holds for the
other standard containers. Generally, you cannot rely on it being size_t.



Actually no, vector<T>::size_type uses the standard allocator, so its
size_type must be size_t.

But things get interesting when we consider custom allocators. Then you are
right that you cannot assume that vector<T, some_allocator>::size_type is
size_t. However the C++ standard explicitly gives permission of
implementations of the standard library to assume that vector<T,
some_allocator>::size_type is size_t (same goes for all the other
containers).

In other words you cannot pass an allocator to a standard library container
type that uses a size_type different from size_t and expect it to work.

I imagine that this strange get out clause for the standard library
implementors was so that existing implementations were not invalidated (but
that''s only a guess).

john


这篇关于size_t和size_type的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆