About: size_t and size_type (transcribed)

In order to make their programs have good portability, C ++ programmers should make use of size_t and size_type, instead of int, unsigned.

Syntax standard C / C ++, only int float char bool basic data types, as size_t, or size_type are later programmer easy to remember as defined Some easy to understand the type of the basic data types of variants.

size_t is to facilitate migration between the system defined, which is an unsigned integer, defined on 32-bit systems: unsigned int; is defined as the 64-bit system unsigned long. size_t count generally used, sizeof operator is size_t result type, the type can accommodate maximum guarantee the established target byte size. It is generally meaning "applies to the number of the memory can hold data items unsigned integer type" Therefore, it is widely used in arrays where subscript and memory management functions and the like. For example: typedef int size_t; size_t is defined as an integer. Because size_t type of data is actually saved an integer, so it can also do addition and subtraction, multiplication and division, can also be converted to int and assigned to a variable of type int. There are similar wchar_t, ptrdiff_t and so on.

size_type class type is a string type and the vector class defined type, save for the length of any string objects or vector objects, the library types size_type defined as unsigned type. string :: size_type it on a different machine, the length can be different, not fixed length, but as long as you use this type is suitable for your program the machine, match the actual machine.

 

size_t and size_type main differences:

1. size_t is globally defined type; size_type STL attribute type is defined in the class. Indicate the length of the container in the use of the STL, we generally use size_type.

2. string :: size_type general type is unsigned int, but a different machine environment possible length differences different lengths win32 and win64; size_t are generally unsigned int

When using the header 3. size_t need <cstddef>; when used requires size_type <string> or <vector>

4. The following are equal length, the length of win32: 4 win64: 8

      sizeof(string::size_type)

     sizeof(vector<bool>::size_type)

     sizeof(vector<char>::size_type)

     sizeof(size_t)

5. Both Contact: When a subscript access elements, vector using vector :: size_type as a subscript type (size_type container concept is, none of the containers can not be used), and the lower right type is the subject of the array size_t

Guess you like

Origin www.cnblogs.com/shen007/p/11386723.html