[英]std::size_t vs size_t vs std::string::size_type
size_t
come from when I don't have anything included? size_t
来自哪里? size_t
== std::size_t
? size_t
== std::size_t
是否合理? size_type
in std
containers ( string::size_type
, vector<T>::size_type
, etc)? std
容器中使用size_type
( string::size_type
, vector<T>::size_type
等)? Where does size_t come from when I don't have anything included in an empty project?
当我没有包含在空项目中的任何内容时,size_t来自哪里?
If you don't have anything included, then you can't use size_t
. 如果您没有包含任何内容,则不能使用
size_t
。 It's defined in <stddef.h>
(and perhaps also in <cstddef>
, if your version of that header puts the definitions in the global namespace as well as std
). 它在
<stddef.h>
定义(也可能在<cstddef>
,如果您的头版本将定义放在全局命名空间和std
)。
Is it reasonable to always assume size_t == std::size_t?
总是假设size_t == std :: size_t是否合理?
Yes. 是。 All types and functions defined by the C library are included in the
std
namespace, as long as you include the appropriate C++ header (eg <cstddef>
rather than <stddef.h>
) 只要包含适当的C ++标头(例如
<cstddef>
而不是<stddef.h>
),C库定义的所有类型和函数都包含在std
命名空间中。
When should I use std:: _ ::size_type?
我什么时候应该使用std :: _ :: size_type?
Do you mean the size_type
types defined in some standard classes and templates such as vector
? 你的意思是在一些标准类和模板中定义的
size_type
类型,例如vector
? You could use those when using those classes if you like. 如果您愿意,可以在使用这些类时使用它们。 In most cases, you'll know that it's the same as
size_t
, so you might as well use that and save a bit of typing. 在大多数情况下,你会知道它与
size_t
相同,所以你不妨使用它并节省一些输入。 If you're writing generic code, where you don't know what the class is, then it's better to use size_type
in case it's not compatible with size_t
. 如果您正在编写通用代码,而您不知道该类是什么,那么最好使用
size_type
,以防它与size_t
不兼容。
For example, you might want to write a container designed to hold more items than can be represented by size_t
. 例如,您可能希望编写一个容器,用于容纳比
size_t
表示的更多的项目。 You might use some kind of big number type to represent the container's size, which isn't convertible to size_t
. 您可以使用某种大数字类型来表示容器的大小,该大小不可转换为
size_t
。 In that case, code like size_t s = c.size()
would fail to compile - you'd need to use Container::size_type
instead. 在这种情况下,像
size_t s = c.size()
这样的代码将无法编译 - 您需要使用Container::size_type
。
Where does size_t
come from when I don't have anything included in an empty project? 当我没有包含在空项目中的任何内容时,
size_t
来自哪里?
size_t
is a base unsigned integer memsize-type defined in the standard library of C/C++ languages. size_t
是在C / C ++语言的标准库中定义的基本无符号整数memsize-type。 It is defined in "stddef.h"
for C
and in <cstddef>
for C++
. 它在
C
"stddef.h"
定义,在C++
<cstddef>
定义。
Types defined by the header file "stddef.h"
are located in the global namespace while <cstddef>
header places the size_t
type in the namespace std
. 头文件
"stddef.h"
定义的类型位于全局名称空间中,而<cstddef>
标头将size_t
类型放在名称空间std
。
"stddef.h"
from C is included into C++ for compatibility, and hence the type can be found in both the global namespace ( ::size_t
) and the std
namespace ( std::size_t
). 来自C的
"stddef.h"
包含在C ++中以实现兼容性,因此可以在全局命名空间( ::size_t
)和std
命名空间( std::size_t
)中找到该类型。
size_t
在<cstddef>
定义,位于std
命名空间中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.