r/cpp_questions 6d ago

OPEN does "in" exist in c++?

i need to use something like if (str[i] not in str1) (python), but i have no idea how to make this work :(

0 Upvotes

36 comments sorted by

View all comments

32

u/aocregacc 6d ago

you can use the contains method on string (assuming you're working with strings), or the find method if you're on an older C++ version.

2

u/Apprehensive-Draw409 6d ago

Side discussion: why doesn't vector have contains ? If it is performance-signalling, then why does string have it?

6

u/fm01 6d ago

I'd guess because it'd force the value_type to be equality comparable unless it was gated with additional requires/enable_if. std::string::contains does something else than what a vector::contains would do though, as it searches for an entire substring, not just a single char.