Traps for C + + string.find () functions

Source: Internet
Author: User

String.find (char *) to find if the string contains substrings

Found: Returns the first index

Not found: Returns a large random number instead of returning negative numbers (this is a pit)

So can't write if (String.find (str) > 0)

This must be written if (String.find (str)! = NPOs)

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, return a special flag in C + + with NPOs representation, I am here NPOs value is 4294967295,
 {
cout << "position is:" << position << Endl;
 }
Else
 {
cout << "not found the flag" + flag;
 } 

Traps for C + + string.find () functions

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.