如何在三元运算符中打破 for 循环
How to break a for loop in Ternary Operator
所以,如果条件为假,一种方法是 cout<<"";
,但我觉得这不是正确的方法,而且 break
似乎不起作用。有什么我应该知道的吗?
template <class T>
T get(const string &prompt) {
cout<<prompt;
T ret;
cin>>ret;
return ret;
}
int main()
{
vector<int>x;
for(int i=0;i<1000;i++) x.push_back((rand()%1200+1200));
int rating=get<int>("Your rating: ");x.push_back(rating);
sortVector(x);
for(int i=0;i<x.size();i++) ((x[i]==rating) ? cout<<"Found your rating on pozition "<<i : cout<<"");
}
三元运算符的每一部分都必须能够计算出相同的类型*,所以你不能有一个部分输出(这将 return cout
对象)而另一个break
的。
相反,在您的情况下,为什么不将条件添加到您的 for 中?
for(int i = 0; i < x.size() && x[i] == rating; i++) {
cout<<"Found your rating on pozition ";
}
但这可能不是您真正想要的
您似乎在尝试查找某个项目在数组中的位置。如果是这种情况,而您只是在寻找第一次出现的情况,我建议您这样做:
int pos;
for(pos = 0; pos < x.size() && x[pos] != rating; pos++) { }
if(pos != x.size()) {
cout<<"Found your rating on pozition " << pos;
} else {
cout << "Couldn't find it!";
}
或者更好,使用 std::find
!
*你也可以在那里投掷。感谢@Lightness!
使用条件运算符并不是您要执行的操作的最佳策略。
简化您的代码。使用 if
-else
.
for(int i = 0; i < x.size(); i++)
{
if (x[i] == rating)
{
std::cout << "Found your rating on position " << i << ".\n";
break;
}
else
{
std::cout << "Still looking for your rating.\n";
}
}
你不知道。
摆脱神秘主义,像其他人一样写出正确的 if
陈述。
您不能在条件句中使用 break
。要使用 non-throwing 条件(根据您的评论出于教育目的)进行破解,您实际上需要在 for
循环中使用它,如下所示:
for (int i = 0; i < x.size(); i = x[i] == rating
? ((cout << "found at " << i), x.size())
: i + 1)
;
这样做是在找到评级时(在评估输出并使用逗号运算符忽略它之后)将 i
推进到 x.size()
(因此循环将终止),否则移动 i
到下一个元素索引 (i + 1
)。换句话说,上面条件运算符的使用相当于:
if (x[i] == rating)
{
std::cout << "found at " << i;
i = x.size(); // poor man's "break"
}
else
i = i + 1;
(您当然可以将代码直接放在由 for
循环控制的语句中... for (size_t i = 0; i < x.size(); ) if (x[i] == ...
),但是这样您就不会使用条件运算符(和可以使用实际休息,也可以在 for
进步声明中使用 ++i
并放弃上面的 else
,此时你有 R Sahu 的答案)。
我在coliru.stacked-crooked.com上放了一个完整的演示程序。
正如我在评论中提到的,条件语句中不能 break
,但可以 throw
。对您期望发生的事件使用异常通常是不受欢迎的(一种为意外事件保留它们的编码约定有助于其他程序员理解您如何期望您的程序运行),但它是可能的:
int main()
try
{
...
for (size_t i = 0; i < x.size(); ++i)
x[i] == rating ? throw i : (void)0;
}
catch (size_t i)
{
std::cout << "found at " << i << '\n';
}
你可以在 coliru here 上 find/run/edit 这个。注意 (void)0
被评估但什么都不做 - 它没有副作用。您可以在那里使用任何表达式 - 0
、'x'
、nullptr
- 但前缀 (void)
强调该值不能也不会被读取和用于任何事情.因此,如果我们不打算使用条件运算符,那么使用 if (x[i] == rating) throw i;
将是等效的、更清晰和更简洁的。
如果您不想自学如何滥用条件运算符,您应该如何实际编写它:
std::cout << "found at " << x.find(rating) - x.begin() << '\n';
通常,当您使用 find
时,您应该检查 x.end()
以查看是否找到了您的值,但在您的情况下,您 100% 知道该值在向量中,因此可以安全地保持代码简单。
GNU statement-expression 扩展是可能的:({ break; })
是类型 void
的表达式。
但是没有理由这样做,除非出现极端的宏黑客行为,无论如何您可能都不应该自己做。
所以,如果条件为假,一种方法是 cout<<"";
,但我觉得这不是正确的方法,而且 break
似乎不起作用。有什么我应该知道的吗?
template <class T>
T get(const string &prompt) {
cout<<prompt;
T ret;
cin>>ret;
return ret;
}
int main()
{
vector<int>x;
for(int i=0;i<1000;i++) x.push_back((rand()%1200+1200));
int rating=get<int>("Your rating: ");x.push_back(rating);
sortVector(x);
for(int i=0;i<x.size();i++) ((x[i]==rating) ? cout<<"Found your rating on pozition "<<i : cout<<"");
}
三元运算符的每一部分都必须能够计算出相同的类型*,所以你不能有一个部分输出(这将 return cout
对象)而另一个break
的。
相反,在您的情况下,为什么不将条件添加到您的 for 中?
for(int i = 0; i < x.size() && x[i] == rating; i++) {
cout<<"Found your rating on pozition ";
}
但这可能不是您真正想要的
您似乎在尝试查找某个项目在数组中的位置。如果是这种情况,而您只是在寻找第一次出现的情况,我建议您这样做:
int pos;
for(pos = 0; pos < x.size() && x[pos] != rating; pos++) { }
if(pos != x.size()) {
cout<<"Found your rating on pozition " << pos;
} else {
cout << "Couldn't find it!";
}
或者更好,使用 std::find
!
*你也可以在那里投掷。感谢@Lightness!
使用条件运算符并不是您要执行的操作的最佳策略。
简化您的代码。使用 if
-else
.
for(int i = 0; i < x.size(); i++)
{
if (x[i] == rating)
{
std::cout << "Found your rating on position " << i << ".\n";
break;
}
else
{
std::cout << "Still looking for your rating.\n";
}
}
你不知道。
摆脱神秘主义,像其他人一样写出正确的 if
陈述。
您不能在条件句中使用 break
。要使用 non-throwing 条件(根据您的评论出于教育目的)进行破解,您实际上需要在 for
循环中使用它,如下所示:
for (int i = 0; i < x.size(); i = x[i] == rating
? ((cout << "found at " << i), x.size())
: i + 1)
;
这样做是在找到评级时(在评估输出并使用逗号运算符忽略它之后)将 i
推进到 x.size()
(因此循环将终止),否则移动 i
到下一个元素索引 (i + 1
)。换句话说,上面条件运算符的使用相当于:
if (x[i] == rating)
{
std::cout << "found at " << i;
i = x.size(); // poor man's "break"
}
else
i = i + 1;
(您当然可以将代码直接放在由 for
循环控制的语句中... for (size_t i = 0; i < x.size(); ) if (x[i] == ...
),但是这样您就不会使用条件运算符(和可以使用实际休息,也可以在 for
进步声明中使用 ++i
并放弃上面的 else
,此时你有 R Sahu 的答案)。
我在coliru.stacked-crooked.com上放了一个完整的演示程序。
正如我在评论中提到的,条件语句中不能 break
,但可以 throw
。对您期望发生的事件使用异常通常是不受欢迎的(一种为意外事件保留它们的编码约定有助于其他程序员理解您如何期望您的程序运行),但它是可能的:
int main()
try
{
...
for (size_t i = 0; i < x.size(); ++i)
x[i] == rating ? throw i : (void)0;
}
catch (size_t i)
{
std::cout << "found at " << i << '\n';
}
你可以在 coliru here 上 find/run/edit 这个。注意 (void)0
被评估但什么都不做 - 它没有副作用。您可以在那里使用任何表达式 - 0
、'x'
、nullptr
- 但前缀 (void)
强调该值不能也不会被读取和用于任何事情.因此,如果我们不打算使用条件运算符,那么使用 if (x[i] == rating) throw i;
将是等效的、更清晰和更简洁的。
如果您不想自学如何滥用条件运算符,您应该如何实际编写它:
std::cout << "found at " << x.find(rating) - x.begin() << '\n';
通常,当您使用 find
时,您应该检查 x.end()
以查看是否找到了您的值,但在您的情况下,您 100% 知道该值在向量中,因此可以安全地保持代码简单。
GNU statement-expression 扩展是可能的:({ break; })
是类型 void
的表达式。
但是没有理由这样做,除非出现极端的宏黑客行为,无论如何您可能都不应该自己做。