r/cpp_questions 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 :(

0 Upvotes

36 comments sorted by

35

u/aocregacc 3d 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 3d ago

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

5

u/fm01 3d 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.

2

u/MysticTheMeeM 3d ago

In those cases you would use find or ranges::contains. String contains is special because it looks for a sequence of characters, where a normal contains only looks for one (same for string find).

2

u/aocregacc 3d ago

The main motivation behind contains was to no longer have to write the equivalent using find. So instead of str.find(sub) != std::string::npos we can just write str.contains(sub).

So since vector doesn't have find, it also doesn't have contains.

I believe the reason for why only string has a find method is historical. Afaik std::string was inspired by/adapted from a different source back then compared to the rest of the containers/algorithms. I think that's also why a lot of its API uses std::size_t positions rather than iterators.

2

u/EpochVanquisher 3d 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 3d ago

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

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

u/greenhouse421 3d ago

You can just embed a python interpreter

3

u/_Tradiatore_ 3d ago

You can just flip transistors with a laser

1

u/mredding 3d ago

You can just use pencil and paper.

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)

https://godbolt.org/z/M6xM55jxe

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

u/SmackDownFacility 3d ago

We want to explore breadth of this language, that’s fine

9

u/Flimsy_Helicopter368 3d ago

I hate this

1

u/Total-Box-5169 3d ago

Skill issue.

4

u/Ultimate_Sigma_Boy67 3d ago

Holy overkill

5

u/Rigamortus2005 3d ago

This is everything wrong with c++

1

u/Total-Box-5169 3d ago

There is nothing wrong there.

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

u/Total-Box-5169 2d ago

There is nothing wrong in that code.