括号前的否定 [英] negation before brackets

查看:108
本文介绍了括号前的否定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是此答案的后续问题.我正在尝试构建一个循环,该循环产生一组三个随机数,直到它们与一组特定的预定义的一组三个任意选择的数匹配为止.

This is a follow-up question to this answer. I am trying to build a loop that produces a set of three random numbers until they match a particular pre-defined set of three arbitrary chosen numbers.

我仍在尝试找出该程序要使用哪些运算符来以任意顺序接受随机数,但没有任何结果.

I'm still trying to figure out what operators to use for the program to accept the random numbers in any order but without any results.

我试图对你

!(first==one && second==two && third==three)

但是它似乎在c ++中不起作用.感谢您的回答.

but it doesn't seem to work in c++. Thanks for your answer.

推荐答案

您尝试的条件暗示firstsecondthird的特定顺序与onetwo相同和three.您可以尝试所有六个排列,但这会使程序变得不可读.更好的解决方案是将值添加到向量,对其进行排序,然后比较相等性,如下所示:

The condition that you tried implies that first, second, and third are in the same specific order as one, two, and three. You could try all six permutations, but that would make for a rather unreadable program. A better solution would be to add values to vectors, sort them, and then compare for equality, like this:

vector<int> a;
a.push_back(first);
a.push_back(second);
a.push_back(third);
vector<int> b;
b.push_back(one);
b.push_back(two);
b.push_back(three);
sort(a.begin(), a.end());
sort(b.begin(), b.end());
if (a == b) ... // values match

这是指向ideone上此代码段的链接.

这篇关于括号前的否定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆