String. find () function in C ++ and string: npos

Source: Internet
Author: User
Query whether string a contains substring B,
Instead of using strA. find (strB)> 0, it is strA. find (strB )! = String: npos
String: size_type pos = strA. find (strB );
If (pos! = String: npos ){}
-------------------------------------------
Int idx = str. find ("abc ");
If (idx = string: npos)
...
In the above Code, the idx type is defined as int, which is incorrect. Even if it is defined as unsigned int, it must be defined as string: size_type.
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 type. Because the default configurator uses size_t as the size_type,-1 is converted to an unsigned integer type, and npos becomes the maximum unsigned value of this type. However, the actual value depends on the actual definition of type 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, in the comparative idx = string: npos, if the idx value is-1, the comparison result may be false because the idx and string: npos types are different.
To determine whether the result of find () is npos, the best way is to directly compare:

If (str. find ("abc") = string: npos ){...}

Error: if (str. find ("abc "))
NOTE: If abc is not found,-1 is returned. If it is not 0, True is returned. 0 is False
---------------------------------------------- // The size_type type returned by the find Function
String s ("1a2b3c4d5e6f7g8h9i1a2b3c4d5e6f7g8ha9i ");
String flag;
String: size_type position;

// The find function returns the subscript position of jk in s.
Position = s. find ("jk ");
If (position! = S. npos) // if not found, a special sign c ++ is returned, which is represented by npos. Here, the npos value is 4294967295,
{
Cout <"position is:" <position <endl;
}
Else
{
Cout <"Not found the flag" + flag;
}

// The find function returns the subscript position of any character in the flag for the first time in s.
Flag = "c ";
Position = s. find_first_of (flag );
Cout <"s. find_first_of (flag) is:" <position <endl;

// Search for string B starting from string s subscript 5, and return the subscript of string B in s
Position = s. find ("B", 5 );
Cout <"s. find (B, 5) is:" <position <endl;

// Find all the positions where the flag appears in s.
Flag = "";
Position = 0;
Int I = 1;
While (position = s. find_first_of (flag, position ))! = String: npos)
{
// Position = s. find_first_of (flag, position );
Cout <"position" <I <":" <position <endl;
Position ++;
I ++;
}

// Find the first position in the flag that does not match s
Flag = "acb12389efgxyz789 ";
Position = flag. find_first_not_of (s );
Cout <"flag. find_first_not_of (s):" <position <endl;

// Reverse lookup, the last position of the flag in s
Flag = "3 ";
Position = s. rfind (flag );
Cout <"s. rfind (flag):" <position <endl;
}

Note:

1. If string sub = "abc";

String s = "cdeabcigld";

S. find (sub), s. rfind (sub): If the two functions are completely matched, the matching index is returned. That is, the current index is returned only when s contains three consecutive letters (abc.

S. find_first_of (sub), s. find_first_not_of (sub), s. find_last_of (sub), s. the four functions find_last_not_of (sub) are used to search for indexes in s that contain any letter in sub.

2. If no query is found, string: npos is returned, which is a large number and its value does not need to be known.

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.