Notes for Using STD: String: Find () and STD: String: NPOs

Source: Internet
Author: User
Int Pos = Str. Find ("\ t ");
If (Pos = string: NPOs) in the above Code, the POs type is defined as int, which is incorrect, even if it is defined as unsigned int, it must be defined as string: size_type.

In C ++, NPOs is defined as follows:

Static const size_type NPOs =-1;

Because string: size_type (defined by the string configurator Allocator) describes size, it must be an unsigned integer. Because the default configurator uses size_t as the size_type,-1 is converted to an unsigned integer, and NPOs becomes the maximum unsigned value of this type. However, the actual value depends on the actual definition of size_type. Unfortunately, these maximum values are different. In fact, (unsigned long)-1 and (unsigned short)-1 are different (provided that the two types have different sizes ). Therefore, the comparative POS ==string: NPOs
If the POs value is-1, the comparison result may be false because the POs type is different from the string: NPOs type.

To determine whether the result of find () is string: NPOs, the best way is to directly compare:

If (Str. Find ("\ t") = string: NPOs ){... }

Reprinted from: http://www.cppblog.com/ming81/archive/2011/04/15/144290.html

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.