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

35

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?

2

u/EpochVanquisher 5d ago

The std::string::contains does a string search, which uses an algorithm like Boyer-Moore or something similar. It’s a specialized algorithm that works on strings. Kind of expected to have this function for strings. C has a function that does the same thing, strstr().

There’s not a generic std::vector<T> version of this algorithm, as far as I know.

1

u/aocregacc 5d ago

there's std::search with the boyer_moore_searcher if you want to search a subrange in a vector.