C language String::size_type Type

Source: Internet
Author: User
Tags posix

String::size_type type

Logically, the size () member function should appear to return an integer value, or an unsigned integer as described in section 2.2, "recommendations." In fact, the size operation returns a value of type String::size_type. We need to do some explaining about this type.

The string class type and many other library types define some partner types (companion types). These partner types make the use of the library type machine-independent (machine-independent). Size_type is one of these partner types. It is defined as having the same meaning as the unsigned type (unsigned int or unsigned long) and is guaranteed to be large enough to store the length of any string object. In order to use the Size_type type defined by the string type, the programmer must add a scope operator to indicate that the type of size_type used is defined by the string class.

Any variable that stores the result of a size operation on a string must be of type String::size_type. It is particularly important not to assign the return value of size to an int variable.

Although we do not know the exact type of string::size_type, we can know that it is the unsigned type (2.1.1). For any given data type, the maximum positive value that its unsigned type can represent is one-fold larger than the corresponding signed. This fact indicates that the string length stored by Size_type is twice times as large as int can store.

Another problem with using int variables is that the representation of an int variable on some machines is too small to even store a string object that is not actually long. For a machine with a 16-bit int type, the int type variable can represent a maximum of 32,767 characters of a string object. A string object that can hold a file's content will easily exceed that number. Therefore, to avoid overflow, the safest way to save a string object size is to use the standard library type string:: Size_type.

--------------------------------------------------------------------------------------------------------

All lookup functions return a size_type type, which is typically the location of the string found and returns String::npos if not found. It is important to note that all comparisons with String::npos must be used with string::size_type, not directly using int or unsigned int.

--------------------------------------------------------------------------------------------------------

The problem stems from this piece of code:

#include <algorithm>
#include <stdio.h>
03
int main ()
05 {
size_t indexs = 1;
size_t PS = 100;
int index =-1;
int p = 100;
Ten printf ("%d\n", Std::min (P,index));
printf ("%d\n", Std::min (Ps,indexs));
return 0;
13}

It's a very simple question, but there's an understanding of the type of size_t.

about the source of size_t :

The data type "socklen_t" and int should have the same length. Otherwise, the BSD socket layer's padding is broken. POSIX started with size_t, Linus Torvalds (he wants to have more people, but obviously not many) trying to explain to them that using size_t is completely wrong, because in 64-bit structures Size_ The length of T and int is not the same, and the length of this parameter (that is, the third parameter of the Accept function) must be the same as int, because this is the BSD socket interface standard. Eventually POSIX's gang found a solution, which was to create a new type of "socklen_t". Linuxtorvalds said it was because they found their mistake but embarrassed to admit it to folks, so another new data type was created.

size_t in C, size_t and ssize_t is a "data type" defined in the standard header file provided by ANSI C, which is not a new data type, not a keyword, but is defined by a typedef from an existing data type.

ANSI C provides a total of 24 header files.

It's defined in <cstddef>.

#include <stddef.h>

Using:: size_t

Using::p trdiff_t

Underneath the/usr/lib/gcc/i486-linux-gnu/4.4/include.

<stddef.h> inside I found a few words:

#ifndef __size_type__

#define __SIZE__TYPE__ Long unsigned int

#endif

#if! (Defined (__gunu__) && defined (size_t))

typedef __size_type__ SIZE_T;

#ifdef __beos

typedef long SSIZE_T

...

Not too clear, but from the Internet to find a good explanation :

The size_t is defined to facilitate porting between systems.

defined as unsigned int on a 32-bit system

defined as unsigned long on a 64-bit system

More accurately, it's a 32-bit unsigned integer on a 32-bit system.

64-bit unsigned integer on a 64-bit system

SIZE_T is generally used to denote a count, such as how much is copied.

The result type of the sizeof operator is size_t,

This type guarantees that the byte size of the largest object being built can be accommodated.

It is roughly "an unsigned integer type that is suitable for measuring the number of data items that can fit in memory."

Therefore, it is widely used in places such as array subscripts and memory management functions.

ssize_t:

This data type is used to indicate the size of a block of data that can be read and written. It is similar to size_t, but must be signed.

Then look at the difference between size_t and Size_type :

I think a sentence summed up very well:

size_t are global, and size_type are associated with containers.

Found the following related files:

/c++/4.3/bits/stl_tree.h,stl_list.h,stl_deque.h is so defined.

typedef size_t SIZE_TYPE

Stl_mutiset.h is defined in this way:

typedef typename _REP_TYPE::SIZE_TYPE Size_type;

So what kind of size_type is it?

The string class type and many other library types define some companion types (companion type). With these companion types, the use of the library type can be machine-independent. Size_type is one of these matching types.

Size_type is defined as having the same meaning as the unsigned type (unsigned int, unsigned long), and is guaranteed to be large enough to store the length of any string object. The size_type type defined by the string type is used instead. The programmer must add action to the operator to indicate that the type of size_type used is defined by the string class.

Why don't we apply an int variable to hold the size of a string?

The problem with the int variable is that the representation of an int variable on some machines is too small to even store a string object that is not actually long. For a machine with a 16-bit int type, the int type variable can represent a maximum of 32,767 characters of a string object. A string object that can hold a file's content can easily exceed that number, so to avoid overflow, the safest way to save the size of a string object is to use the standard library type String::size_type ().

Note: While learning the standard library string, I bumped into the size_type type, but, in fact, the vector library can also define the Size_type type, there is a difference_type type in the vector library, This type is used to store the distance between any two iterator objects, so it is of type signed.

So much to say, in fact, the text of this issue in the Scarlet Letter part is enough. Other look at the deeper impression can be, the beginning of the program results output is:

-1 100.

C language String::size_type Type

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.