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> | ||
typedef /*implementation-defined*/ size_t; |
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).
| The bit width of | (since 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. 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 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.
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.
| The integer literal suffix for | (since C++23) |
#include <cstddef>
#include <iostream>
#include <array>
int main()
{
std::array<std::size_t, 10> a;
// Example with C++23 size_t literal
for (auto i = 0uz; i != a.size(); ++i)
std::cout << (a[i] = i) << ' ';
std::cout << '\n';
// Example of decrementing loop
for (std::size_t i = a.size(); i--;)
std::cout << a[i] << ' ';
// Note the naive decrementing loop:
// for (std::size_t i = a.size() - 1; i >= 0; --i) ...
// is an infinite loop, because unsigned numbers are always non-negative
}Output:
0 1 2 3 4 5 6 7 8 9 9 8 7 6 5 4 3 2 1 0
| signed integer type returned when subtracting two pointers (typedef) |
|
| byte offset from the beginning of a standard-layout type to specified member (function macro) |
|
| integer literals | binary, (since C++14) decimal, octal, or hexadecimal numbers of integer type |
C documentation for size_t |
|
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/types/size_t