Use std::find :
std::size_t listsize = sizeof mylist / sizeof mylist[0]; if (std::find(mylist, mylist + listsize, myinput) != mylist + listsize) {
If you know the size of the list in advance, I suggest std::array , which provides iterators and the size() function, as well as several other advantages over built-in arrays. Note that this is only C ++ 11 (the equivalent of C ++ 03 is std::vector ), and also with C ++ 11 comes std::begin and std::end , which reduces it to the following:
if (std::find(std::begin(mylist), std::end(mylist), myinput) != std::end(mylist))
In C ++ 03, it's quite simple to make your own for inline arrays, but with a standard container that provides the begin() and end() members, this should not be too necessary, although it is more universal.
source share