В C ++ 0x:
#include <algorithm>
#include <iterator>
#include <string>
bool is_offered(const std::string& s)
{
// look up table
static const std::string toppingsOffered[] =
{"onions", "bell peppers", /* etc */ };
const auto toppingsBegin = std::begin(toppingsOffered);
const auto toppingsEnd = std::end(toppingsOffered);
return std::find(toppingsBegin, toppingsEnd, s) != toppingsEnd;
}
if (is_offered())
std::cout << "yes";
В C ++ 03:
#include <algorithm>
#include <string>
bool is_offered(const std::string& s)
{
// look up table
static const std::string toppingsOffered[] =
{"onions", "bell peppers", /* etc */ };
const std::string* toppingsBegin = &toppingsOffered[0];
const std::string* toppingsEnd =
toppingsBegin +
sizeof(toppingsOffered) / sizeof(std::string);
return std::find(toppingsBegin, toppingsEnd, s) != toppingsEnd;
}
if (is_offered(topping))
std::cout << "yes";
В C ++ 03 с утилитами:
#include <algorithm>
#include <cstddef>
#include <string>
template <typename T, std::size_t N>
T* begin(T (&array)[N])
{
return *array[0];
}
template <typename T, std::size_t N>
T* end(T (&array)[N])
{
return begin(array) + N;
}
bool is_offered(const std::string& s)
{
// look up table
static const std::string toppingsOffered[] =
{"onions", "bell peppers", /* etc */ };
const std::string* toppingsBegin = begin(toppingsOffered);
const std::string* toppingsEnd = end(toppingsOffered);
return std::find(toppingsBegin, toppingsEnd, s) != toppingsEnd;
}
if (is_offered(topping))
std::cout << "yes";