检查循环中出现的任何条件是否已满足c++
check if any of the conditions that occur in the loop have been fulfilled c++
如何使用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";
未选中的代码。
相关文章:
- 无论条件是否为true,if总是在c++中执行
- Vulkan 的传输队列系列功能和显卡支持:条件检查是否准确?
- 是否可以在C++中进入更高级别的范围条件?
- 是否可以为一个循环设置多个条件
- 在if条件下,右或左改变值的相等性检查是否有任何区别
- 在C++中,是否可以编写一个条件的lambda,即只是一个计算结果为真或假的条件
- 如果条件取决于模板类型并且在编译时已知,是否可以保证C++编译器不会生成分支?
- 如何检查条件是否为 true 5 秒?
- 是否可以编写C++不使用任何条件语句交换输入的函数?
- 是否可以在条件之前对加载或存储进行重新排序?
- 是否根据编译器版本有条件地包含源文件
- C :是否值得使用static_cast避免使用数字的条件
- 是否可以删除此条件语句
- 在不再满足条件后,while 循环是否停止验证?
- 如果检查和内联条件之间是否存在编译器差异
- __has_include() 和后续 #include 之间是否存在争用条件
- 是否可以使用松弛的内存顺序来观察条件?
- 有没有办法有条件地检查窗口是否正在使用滚动条
- 是否可以在 for 循环条件括号内使用三个分号?
- 有条件地添加到过载集是否合法