negation before brackets

2019-09-12 16:00发布

问题:

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.