检查循环中出现的任何条件是否已满足c++

check if any of the conditions that occur in the loop have been fulfilled c++

本文关键字:是否 条件 满足 c++ 任何 循环 检查      更新时间:2023-10-16

如何使用for循环编写以下代码?即,如何使用for循环来检查循环中出现的任何条件是否已满足?我知道一定有办法,我相信可能有人在SO上问过这个问题,但我不太确定该怎么表达。所以如果有重复的,也许你可以给我指明正确的方向。

  string topping;
  cout << "Enter a topping ";
  cin >> topping;
  string toppings_offered[5] = {"onions", "bell peppers", "olives", "spinach", "tomatoes"};
  if ( (topping == toppings_offered[0]) || (topping == toppings_offered[1]) || (topping == toppings_offered[2]) || (topping == toppings_offered[3]) || (topping == toppings_offered[4]))
           cout << "yes";

一点逻辑理论:

所有满足的条件都是不满足的一个条件的倒数。

在for((循环中,如果其中一个条件不满足,则答案为false。否则,这是真的。

然而,我不相信你问的问题是正确的,因为在你的例子中,一个浇头只能匹配一个浇头,而不是所有浇头。

在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";
bool isok = false;
for(...)
{
    if(cond)
        isok = true;
}
if(isok)
    cout << "yes"
for (int i = 0; i < 5; i++)
    if (topping == toppings_offered[i])
    {
        cout << "yes";
        break;
    }

按照你的要求去做。你不需要检查他们是否都完成了。您需要检查输入的浇头是否为提供的浇头之一。至少这就是代码的含义。

希望它能有所帮助!

int i;
for( i = 0; i < sizeof(toppings_offered) / sizeof(string); i++)
{
    if(topping == toppings_offered[i])
    {
       cout << "yes";
    }
}

伪代码:

  result = false
  for i = 1 to n do
     result = result or (input = target[i])
  if result then print("At least one matched")
  else then print("None matched")

类似的代码可以用来查看是否所有匹配或至少有一个不匹配。

您的意思是,如果其中一个条件已经满足。它们不可能全部实现,尽管它们都可以检查。

int i = 0;
for (; i < 5; ++i)
  if (topping == toppings_offered[i])
    break;
if (i < 5)
  cout << "yes";

未选中的代码。