String: size_type in C ++

Source: Internet
Author: User

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)

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.