negation before brackets

淺唱寂寞╮ 提交于 2019-12-11 18:12:28

问题


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.

I tried to your

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

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


回答1:


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

Here is a link to this snippet on ideone.



来源:https://stackoverflow.com/questions/12182551/negation-before-brackets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!