- Where does
size_t
come from when I don't have anything included? - Is it reasonable to always assume
size_t
==std::size_t
? - When should I use the
size_type
instd
containers (string::size_type
,vector<T>::size_type
, etc)?
当我没有包含任何东西时,size_t来自哪里?
总是假设size_t == std :: size_t是否合理?
我什么时候应该在std容器中使用size_type(string :: size_type,vector
3 个解决方案
#1
10
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
. 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
).
如果您没有包含任何内容,则不能使用size_t。它在
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 (e.g. <cstddef>
rather than <stddef.h>
)
是。只要包含适当的C ++头(例如
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
? 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. 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类型,例如vector?如果您愿意,可以在使用这些类时使用它们。在大多数情况下,你会知道它与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
. You might use some kind of big number type to represent the container's size, which isn't convertible to 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表示的更多的项目。您可以使用某种大数字类型来表示容器的大小,该大小不可转换为size_t。在这种情况下,像size_t s = c.size()这样的代码将无法编译 - 您需要使用Container :: size_type。
#2
1
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. It is defined in "stddef.h"
for C
and in <cstddef>
for C++
.
size_t是在C / C ++语言的标准库中定义的基本无符号整数memsize-type。它在C的“stddef.h”中定义,在C ++的
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”定义的类型位于全局名称空间中,而
"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)中找到该类型。
#3
0
size_t
is defined in <cstddef>
and is in std
namespace.
size_t在
#1
10
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
. 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
).
如果您没有包含任何内容,则不能使用size_t。它在
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 (e.g. <cstddef>
rather than <stddef.h>
)
是。只要包含适当的C ++头(例如
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
? 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. 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类型,例如vector?如果您愿意,可以在使用这些类时使用它们。在大多数情况下,你会知道它与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
. You might use some kind of big number type to represent the container's size, which isn't convertible to 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表示的更多的项目。您可以使用某种大数字类型来表示容器的大小,该大小不可转换为size_t。在这种情况下,像size_t s = c.size()这样的代码将无法编译 - 您需要使用Container :: size_type。
#2
1
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. It is defined in "stddef.h"
for C
and in <cstddef>
for C++
.
size_t是在C / C ++语言的标准库中定义的基本无符号整数memsize-type。它在C的“stddef.h”中定义,在C ++的
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”定义的类型位于全局名称空间中,而
"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)中找到该类型。
#3
0
size_t
is defined in <cstddef>
and is in std
namespace.
size_t在