C + + string::size_type type "Go"

Source: Internet
Author: User
Tags posix

1 intMain ()2    {3        stringStr"Hello world!\n");4cout <<"The size of"<< Str <<" is"<<str.size ()5<<"characters, including the newline"<<Endl;6        return 0;7}
View Code

Logically speaking,size()

The member function should appear to return an integer value or an unsigned int. In fact, thesize operation returns a value of type string::size_type .

&NBSP;&NBSP; 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 (machine-independent). size_type is one of these companion types. It is defined as   unsigned   type ( unsigned int   or   unsigned long ) has the same meaning, It is also guaranteed to be large enough to store any   string   The length of the object. In order to use   defined by   string   type, size_type   type is defined by   string   class. Any storage string   type. It is particularly important to size the return value of to a   int   variable.

Although we do not know the exact type of String::size_type , we can know that it is the unsigned type. For any given data type, the maximum positive value that its unsigned type can represent is one-fold larger than the corresponding signed type. 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, theint 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 stirng object size is to use the standard library type string::size_type.

string str ("some string" for (string0; Index! = str.size (); + +index     )<< Str[index] << Endl;

From http://blog.sina.com.cn/s/blog_4cb9953f0100duon.html

=================================================================================================

The same knowledge points are described in the article:

From http://xueqi.iteye.com/blog/1569713

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:

01#include <algorithm>
02#include <stdio.h>
03
04IntMain ()
05{
06size_t indexs =-1;
07size_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;
- }

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". Linux Torvalds 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

Under/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 so directly 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.

====================================================================================

Below from: http://www.cnblogs.com/debuging/archive/2012/07/27/2612090.html

About String::size_type

Size_type is actually one of the types defined by the string template class, similar to the use of size_of, except that it returns the length of a string based on the type of storage. For string materialization, the length of the string is returned based on Char, in which case the Size_ Type is equivalent to size_of. Size_of is an unsigned type.
The size_type you're talking about is actually the unsigned int type is actually not correct, it depends on the type of storage.

Size_type is actually introduced in order to solve serious errors in the design of the string class.

Size_type is a type of unsigned shaping defined within the string class, because the return value of many methods in the string class is of type Size_type, you must use Size_type for the type declaration.
As follows:
String a= "ABCDEFG";
String::size_type idx;
IDX = A.find ("h"); Not Found,return String::npos
if (idx = = String::npos)//check if not find
{
.....
}

Because NPOs returns a special value (-1) for Find, unfortunately, the Size_type type is unsigned int, so
Different compilers have different understandings of NPOs.
Imagine using 8 bytes to unsigned int to represent-1 and 4 bytes, the value is completely different.
This could be a fatal flaw in the design of the string class, for compatibility and portability,
Specific size_type types, in different compilers, the interpretation of Size_type is also different, so that it can be portable.

The above content is visible
Stl:11.2.12 Festival.

C + + string::size_type type "Go"

Related Article

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.