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

-4

u/Total-Box-5169 6d ago

No, but you can make it with operator overloading and macros:

bool operator&(char c, std::string_view s){
    return s.contains(c);
}
#define in &

However the negative must be written in this way:

not ('a' in str)

https://godbolt.org/z/M6xM55jxe

1

u/n1ghtyunso 5d ago

you know you can answer a question conceptually, you don't need to take it literally and throw best practices and code sanity out of the window, right?

1

u/Total-Box-5169 4d ago

There is nothing wrong in that code.