1

Given an int array database, instead of

if (user_input == int database[0] || user_input == int database[1])
{
    // ...
}

Could I do something more concise, like this?

if (user_input == any variable from database)
{
    // ...
}
L. F.
  • 19,445
  • 8
  • 48
  • 82
baguettio
  • 179
  • 1
  • 1
  • 8
  • 1
    Is [std::any_of](https://en.cppreference.com/w/cpp/algorithm/all_any_none_of) what you are looking for, maybe? – Jesper Juhl Feb 01 '20 at 14:30
  • Or implement the database as [std::unordered_set](https://en.cppreference.com/w/cpp/container/unordered_set) and then you can simply do `if (database.find(user_input) != database.end()) { /* element exists */ }` – David C. Rankin Feb 01 '20 at 14:33
  • Does this answer your question? [How to find out if an item is present in a std::vector?](https://stackoverflow.com/questions/571394/how-to-find-out-if-an-item-is-present-in-a-stdvector) (Replace `vec.begin()` and `vec.end()` with `std::begin(database)` and `std::end(database)`) – L. F. Feb 01 '20 at 14:39
  • @JesperJuhl If an array had the integers 1 2 and 3 in it, and I used std::any_of to compare that and another variable, would it come out true if the variable was 1 2 or 3, but false if it was 4? – baguettio Feb 01 '20 at 14:49
  • 1
    @baguettio Yes. – Ted Lyngmo Feb 01 '20 at 14:50
  • @TedLyngmo That is exactly what am I looking for thank you – baguettio Feb 01 '20 at 14:56
  • 1
    I redirect that _thank you_ to @JesperJuhl :-) – Ted Lyngmo Feb 01 '20 at 14:58
  • Thank *you* @JesperJuhl – baguettio Feb 01 '20 at 15:02

1 Answers1

0

What you probably are looking for is std::find algorithm.

E.g.

if(std::end(database) != std::find(std::begin(database), std::end(database), user_input)) { ... }
Maxim Egorushkin
  • 131,725
  • 17
  • 180
  • 271
  • @JesperJuhl You should learn how to find an integer in an array then. – Maxim Egorushkin Feb 01 '20 at 14:36
  • I do know how to find integers in arrays, thank you very much. But OP *seems* to want to find if some variable is `any_of` some set - quoting OP "If (user_input == any variable from ..." – Jesper Juhl Feb 01 '20 at 14:39
  • @JesperJuhl The OP wants to find `user_input` in `int database[]`. That's what `std::find` is for. – Maxim Egorushkin Feb 01 '20 at 14:40
  • This is clearly wrong. OP is looking for `std::any_of`. – super Feb 01 '20 at 14:42
  • @super `std::any_of` is longer to type and read but would have the same effect as shorter `std::find`. Just because question has _any_ in it doesn't mean one should use `std::any_of`. – Maxim Egorushkin Feb 01 '20 at 14:44
  • 2
    @MaximEgorushkin It's 5-6 characters difference on a 100 character-ish line, that would not convince me to trade the more clearly expressed intent of `std::any_of`. – super Feb 01 '20 at 14:56
  • `std::any_of` expresses the *intent* much clearer. That's, in my opinion, much more important than saving on typing (which I would consider irrelevant). Code is *read* much more than it is written, being clear for readers is the primary concern. – Jesper Juhl Feb 01 '20 at 14:57
  • @super That is you who should convince using `std::any_of` instead of `std::find`. – Maxim Egorushkin Feb 01 '20 at 14:57
  • @JesperJuhl Using `std::any_of` requires you typing otherwise unnecessary lambda expression, which makes your code less clear, then when using `std::find` whose name tells the reader exactly what it does without requiring the reader to examine the lambda expression to figure our what it does. – Maxim Egorushkin Feb 01 '20 at 15:00
  • 2
    @JesperJuhl I agree to disagree agreeably. – Maxim Egorushkin Feb 01 '20 at 15:01