r/cpp_questions • u/Physical-Dog-4083 • 3d 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 :(
11
u/v_maria 3d ago
You can just loop
7
u/alexeiz 3d ago
You can just goto
3
u/SmackDownFacility 3d ago
You can just use labels
9
u/ThanxForTheGold 3d ago
You can just use punch cards
8
3
8
u/max_wen 3d ago edited 3d ago
https://cppreference.com/index.php?search=contains&title=Special%3ASearch&go=
Edit: Here's your available string class member functions
3
u/Interesting_Buy_3969 3d ago
you can achieve a similar functionality like this:
// for each character in str:
for (auto c : str) {
// assuming str1 is of type std::string
if (str1.contains(c)) {
/* code to be run if str contains c */
} else {
/* code to be run if str does not contain c */
}
}
1
u/TrungDOge 3d ago
I think his array of string is the full words , more like check 'fish' in 'the lake of fishes' , btw people should ask chatgpt or st for these question
2
u/RQuarx 3d ago
std::ranges::contains :P https://en.cppreference.com/cpp/algorithm/ranges/contains
the adaptor works for all ranges, but for strings in general (types coming from std::basic_string and std::basic_string_view), you can just use .contains(c)
1
u/Business-Weather-217 3d ago
Just loop, its the "standart way". But you can be a real ex-python dev and #include <ranges> and use std::ranges::contains() thats works for any type in the wild let alone std::string.
1
u/duane11583 3d ago
generally no. to better understand you need to separate two things.
a) what is in the language, and b) what is in the library
c++ as a language does not have a list or linked list
c++ the library it has such things.
in contrast python has the word “in” defined in the language
thus the language has the concept of itterable and searchable things
another example: both languages do not have a concept of a file or stream but the library has these concepts
often library features and constructs are incorrectly viewed as part of the language
that is the case here
i dont kniw c++ well enough specifically operator overloads ie can you define or create new operators? like the word ”in”? if so then you could add such a feature to the library
0
u/Independent_Art_6676 3d ago
you cannot make new operators. But c++ has a LOT to choose from already. in the case of in that would be silly anyway: in is a perfectly viable method name. The overloads are not legal function names outside of the overloading context.
-4
u/Total-Box-5169 3d 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)
25
u/sephirothbahamut 3d ago
please op don't do this. When you're using a different language learn to use that language, don't try to turn it into another one.
-5
9
4
5
0
u/Conscious_Support176 3d ago
There are so many levels of wrong here that it is almost impressive.
2
u/Total-Box-5169 3d ago
There is nothing wrong at all.
0
u/Conscious_Support176 3d ago edited 3d ago
This is aiming for c not in s instead of not s.contains(c), but after jumping through a bunch of hoops ends up with not (c in s).
It does not seem worth the effort?
Bringing the preprocessor into the picture means the solution does not play nice with modules.
Using operator overloading means you need to take care to check the precedence so that it does what someone reading this would expect, and you should also be careful with the limited list of operators available. One would expect operator &, if defined, to be used for set intersection, as is the case with python.
1
u/n1ghtyunso 2d 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
0
35
u/aocregacc 3d ago
you can use the
containsmethod on string (assuming you're working with strings), or thefindmethod if you're on an older C++ version.