+ 2

Is there an "in" like function in c++

I'm working on a project on c++ and I couldn't use "in" function in it, Is there anyone who knows?

9th Sep 2020, 9:48 AM
Mohammed Ali
Mohammed Ali - avatar
4 odpowiedzi
+ 2
For example, in python you can do like this: if ("h" in "hello") Print (True) And so on
9th Sep 2020, 10:02 AM
Mohammed Ali
Mohammed Ali - avatar
+ 1
Mohammed Ali , You can use std::any_of() method. std::string str = "hello"; bool hasCharH = std::any_of(str.begin(), str.end(), [](char c){return c=='h';}); if(hasCharH){ std::cout<<"Contains h"; }else{ std::cout<<"Does not contain h"; } Make sure you #include <algorithm>. https://devdocs.io/cpp/algorithm/all_any_none_of
9th Sep 2020, 10:14 AM
🇮🇳Omkar🕉
🇮🇳Omkar🕉 - avatar
0
Mohammed Ali , string does not define `in` method. Can you describe what problem you are trying to solve? What you are expecting `in` method to do?
9th Sep 2020, 9:57 AM
🇮🇳Omkar🕉
🇮🇳Omkar🕉 - avatar
0
Mohammed Ali You can use std::find() found in <algorithm>
9th Sep 2020, 8:08 PM
Anthony Maina
Anthony Maina - avatar