C++ - C -
size_t
size_t
1. C - size_t
typedef /* implementation-defined */ size_t
Defined in header <stddef.h>
Defined in header <stdio.h>
Defined in header <stdlib.h>
Defined in header <string.h>
Defined in header <time.h>
Defined in header <uchar.h> (since C11)
Defined in header <wchar.h> (since C95)
Unsigned integral type - 无符号整数类型
Alias of one of the fundamental unsigned integer types.
基础的无符号整数类型的别名之一。
It is a type able to represent the size of any object in bytes: size_t
is the type returned by the sizeof
operator and is widely used in the standard library to represent sizes and counts.
它是一种能够以字节为单位表示任何对象的大小的类型:size_t
是由 sizeof
运算符返回的类型,并且在标准库中广泛用于表示大小和计数。
size_t
can store the maximum size of a theoretically possible object of any type (including array).
size_t
能存储理论上可行的任何类型 (包括数组) 对象的最大大小。
size_t
is commonly used for array indexing and loop counting. Programs that use other types, such as unsigned int
, for array indexing may fail on, e.g. 64-bit systems when the index exceeds UINT_MAX
or if it relies on 32-bit modular arithmetic.
size_t
通常用于数组下标和循环计数。使用 unsigned int
的其他类型用作数组下标的的程序,可能在 64 位系统上失败,例如在下标超过 UINT_MAX
时,或若依赖 32 位模算术。
2. C - Example
2.1 size_t
//============================================================================
// Name : size_t
// Author : Yongqiang Cheng
// Version : Version 1.0.0
// Copyright : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <stdio.h>
#include <stddef.h>
#include <stdint.h>
int main(void)
{
const size_t N = 100;
int numbers[N];
for (size_t ndx = 0; ndx < N; ++ndx)
{
numbers[ndx] = ndx;
}
printf("SIZE_MAX = %zu\n", SIZE_MAX);
size_t size = sizeof numbers;
printf("size = %zu\n", size);
return 0;
}
Possible output:
SIZE_MAX = 18446744073709551615
size = 400
3. C++ - std::size_t
typedef /* implementation-defined */ size_t;
Defined in header <cstddef>
Defined in header <cstdio>
Defined in header <cstdlib>
Defined in header <cstring>
Defined in header <ctime>
Defined in header <cuchar> (since C++17)
Defined in header <cwchar>
In <cstdio>
, it is used as the type of some parameters in the functions fread
, fwrite
and setvbuf
, and in the case of fread
and fwrite
also as its returning type.
在 <cstdio>
中,它用作函数 fread
, fwrite
and setvbuf
中某些参数的类型,在 fread
and fwrite
的情况下,还用作其返回类型。
std::size_t
is the unsigned integer type of the result of the sizeof
operator as well as the sizeof...
operator and the alignof
operator (since C++11).
std::size_t
是 sizeof
运算符还有 sizeof...
运算符和 alignof
运算符 (C++11 起) 所返回的无符号整数类型。
The bit width of std::size_t
is not less than 16. (since C++11)
std::size_t
的位宽不小于 16。(C++11 起)
std::size_t
can store the maximum size of a theoretically possible object of any type (including array). A type whose size cannot be represented by std::size_t
is ill-formed (since C++14) On many platforms (an exception is systems with segmented addressing) std::size_t
can safely store the value of any non-member pointer, in which case it is synonymous with std::uintptr_t
.
std::size_t
可以存放下理论上可能存在的对象的最大大小,该对象可以是任何类型,包括数组。大小无法以 std::size_t
表示的类型是不规范的。(C++14 起) 在许多平台上(使用分段寻址的系统除外),std::size_t
可以存放下任何非成员的指针,此时可以视作其与 std::uintptr_t
同义。
std::size_t
is commonly used for array indexing and loop counting. Programs that use other types, such as unsigned int
, for array indexing may fail on, e.g. 64-bit systems when the index exceeds UINT_MAX
or if it relies on 32-bit modular arithmetic.
std::size_t
通常被用于数组索引和循环计数。使用其它类型来进行数组索引操作的程序可能会在某些情况下出错,例如在 64 位系统中使用 unsigned int
进行索引时,如果索引号超过 UINT_MAX
或者依赖于 32 位取模运算的话,程序就会出错。
When indexing C++ containers, such as std::string
, std::vector
, etc, the appropriate type is the member typedef size_type
provided by such containers. It is usually defined as a synonym for std::size_t
.
在对诸如 std::string
, std::vector
等 C++ 容器进行索引操作时,正确的类型是该容器的成员 typedef size_type
,而该类型通常被定义为与 std::size_t
相同。
synonym ['sɪnənɪm]:n. 同义词
4. C++ - Example
4.1 std::size_t
//============================================================================
// Name : std::size_t
// Author : Yongqiang Cheng
// Version : Version 1.0.0
// Copyright : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <cstddef>
#include <iostream>
int main()
{
const std::size_t N = 10;
int *a = new int[N];
for (std::size_t n = 0; n < N; ++n)
{
a[n] = n;
}
std::cout << "A:" << std::endl;
for (std::size_t n = N; n-- > 0;)
{
std::cout << a[n] << " ";
}
std::cout << std::endl << "B:" << std::endl;
for (std::size_t n = N; --n > 0;)
{
std::cout << a[n] << " ";
}
std::cout << std::endl << "C:" << std::endl;
for (std::size_t n = N - 1; n > 0; n--)
{
std::cout << a[n] << " ";
}
std::cout << std::endl << "D:" << std::endl;
for (std::size_t n = N - 1; n > 0; --n)
{
std::cout << a[n] << " ";
}
delete[] a;
return 0;
}
A:
9 8 7 6 5 4 3 2 1 0
B:
9 8 7 6 5 4 3 2 1
C:
9 8 7 6 5 4 3 2 1
D:
9 8 7 6 5 4 3 2 1
4.2 std::size_t
//============================================================================
// Name : std::size_t
// Author : Yongqiang Cheng
// Version : Version 1.0.0
// Copyright : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <cstddef>
#include <iostream>
#include <array>
int main()
{
std::array<std::size_t, 10> a;
for (std::size_t i = 0; i < a.size(); ++i)
{
a[i] = i;
}
std::cout << "a.size() = " << a.size() << std::endl;
for (std::size_t idx = a.size(); idx-- > 0;)
{
std::cout << a[idx] << " ";
}
std::cout << std::endl << std::endl;
std::size_t ndx;
for (ndx = a.size() - 1; ndx < a.size(); --ndx)
{
std::cout << "ndx = " << ndx << " " << std::endl;
std::cout << a[ndx] << std::endl;
}
std::cout << std::endl;
std::cout << "ndx = " << ndx << " " << std::endl;
return 0;
}
a.size() = 10
9 8 7 6 5 4 3 2 1 0
ndx = 9
9
ndx = 8
8
ndx = 7
7
ndx = 6
6
ndx = 5
5
ndx = 4
4
ndx = 3
3
ndx = 2
2
ndx = 1
1
ndx = 0
0
ndx = 18446744073709551615
务必注意无符号数的特性。
Reference
https://en.cppreference.com/w/c/types/size_t
https://en.cppreference.com/w/cpp/types/size_t
http://www.cplusplus.com/reference/cstdio/size_t/