开发者

Is the integer n in the string s? Is 4 in Ab56c4D? (C++)

开发者 https://www.devze.com 2023-01-29 13:44 出处:网络
Is there a standard f开发者_C百科unction which will return a bool for this? I\'m writing a program that plays the game of life and the user enters two strings, S23 and B3 are examples. In my main loo

Is there a standard f开发者_C百科unction which will return a bool for this?

I'm writing a program that plays the game of life and the user enters two strings, S23 and B3 are examples. In my main loop I just want to check if an integer (the number of living surround cells) is in one of the strings.

Thanks for your help with this question. ;)


http://www.cplusplus.com/reference/string/string/find/

Searches the string for the content specified in either str, s or c, and returns the position of the first occurrence in the string.

Return Value: The position of the first occurrence in the string of the searched content. If the content is not found, the member value npos is returned.


First you need to get a string version of the integer value, then you can try to find it in the other string:

std::ostringstream oss;
oss << some_integer;
if (some_string.find(oss.str()) != std::string::npos)
    // match...


Loop through the characters in the string and if the character ( (int)cur_char) ) is between 48 and 57 return true.

0

精彩评论

暂无评论...
验证码 换一张
取 消