简体   繁体   中英

cpp string find() doesnt work as expected - returns big junk values

I'm trying to find the index of a substring in another string, using find(), but getting a junk value and not std::npos if the substring is not there.

This is the Code:

string output1 = "abcd";

cout << output.find("gf") << endl;

And this is the output:

18446744073709551615

can this behaviour be prevented? is there another way to find the substring?

(actually i only need to find if the substring is contained)

Thank you

What it return is size_t of npos of your string because it can't find your char or text. you can do this instead:

std::size_t found = str.find("findme");

if (found != std::string::npos)
    std:cout << found << std::endl;
else
    std::cout << "String not found" << std::endl // If not found

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM