The string class type and many other library types are definedCompanion type ).With these supporting types, the use of the library type can be unrelated to the machine-independent ). String: size_type is defined as the unsigned type, which can ensure that the length of the string object is large enough.
Note: any variable that stores the size operation result of string must be of the string: size_type type. In particular, the return value of size cannot be assigned to an int variable. (Because the size returns an unsigned type, and the int type is signed. The value of size is twice the value of Int ).
String STR ("some string ");
For (string: size_type IX = 0; ix! = Str. Size (); ++ IX) // If IX is of the int type, use <=
{
Cout <STR [ix] <Endl;
}
Vector <int> ivec;
For (vector <int >:: iterator IX = ivec. Begin (); ix! = Ivec. End (); ++ IX)
// Int is not used here! =, Instead of <=
{
* IX = 0; // assign each element to 0.
}
(Post by Wang Jie)