C language size_t and compare size_type

size_tsize_t is the index for the array value types can also be used "receives" the sizeof operator returns the value.

1, since it is unsigned ,, generally only be used where there is no negative, and such as we age ah, height ah. In the c standard functions, the most common is strlen, returns the number of characters. Of course, the number of characters can not be negative ah, the function prototype is size_t strlen (const char *); size_t is a length (size) of the type, which is a unsigned typedef int size_t; defined, typically used to hold some of the length information such as the length, the length of the string array and the like;

2, size_type is supporting container type, before use add scopes such as string :: size_type (string of characters can be seen as a container, but not the class template) and other defined declare an array of type size_t should be used, if you have to use an int as a subscript It should be used to avoid cross-border unsigned. As size_type type can not be converted ...... but maybe a string stream can be tried.

example:

Examples of application of array size_t

Problem Description:

Number inverted output array

Code:

#include

using namespace std;

int main ()

{

const size_t a_size = 10;

int a[a_size];

for (size_t i = 0; i != a_size; i++)

{

a[i] = i;

}

for (int j = a_size - 1; j >= 0; j–)

{

cout << a[j] << endl;

}

return 0;

}

Results are as follows:

9 8 7 6 5 4 3 2 1 0

3. Summary:

size_type is a container concept, no vessel can not use the size_t is actually unsigned int

Published 240 original articles · won praise 3 · Views 3179

Guess you like

Origin blog.csdn.net/it_xiangqiang/article/details/105163719
Recommended