-1

I'm new to C++ and I'm trying to compare two unsorted vectors elements against each other and determining if they match or not.

for example: 1 4 9 16 9 7 4 9 11 and 11 11 7 9 16 4 1 these would be considered a match.

I've tried using firstVector == secondVector, but that does not work when they vectors are not sorted, I'm specifically trying to compared them while they are unsorted but am struggling.

int main() {
int incorrectMatches = 0;
int totalMatches = 0;
int input = 0;
vector <int> firstVector;
vector <int> secondVector;

do {
    cout << "Please enter a number for first vector:  ";
    cin >> input;
    firstVector.push_back(input);
    cout << endl;
} 

while (input > 0);

input = 0;

firstVector.pop_back();

do {
    cout << "Please enter a number for second vector: ";
    cin >> input;
    secondVector.push_back(input);
    cout << endl;
} 

while (input > 0);


secondVector.pop_back();



for (int loop = 0; loop < firstVector.size(); loop++) {
    cout << firstVector[loop] << " ";
}

cout << endl;
for (int loop = 0; loop < secondVector.size(); loop++) {
    cout << secondVector[loop] << " ";
}

cout << endl;
int vectorSize = firstVector.size();

for (int i = 0; i < vectorSize; i++) {
    if (firstVector[i] == secondVector[i]) {
        totalMatches = totalMatches++;
    }
    else {
        incorrectMatches = incorrectMatches++;
    }
}

cout << "There were " << totalMatches << " matches." << endl;

cout << "There were " << incorrectMatches << " incorrect matches";

/*
if (firstVector == secondVector) {
    cout << "Your vectors match!";
}

else{
    cout << "Your vectors don't match!";
}

*/
/*
for (int i = 0; i < input; i++) {
    cout << "Please enter a number (1-9): ";
    myVector.push_back(input);
    cout << endl;

    myVector[i] = i;

    cout << "Vector entry: " << myVector[i] << endl;

}

*/

system("pause");
return 0;}
Cambino
  • 3
  • 1
  • 2
  • 2
    How about sorting both with `std::sort`, then comparing them with `==`? It's as efficient as most other strategies (common exception being using `std::unordered_map` to count frequencies) and is very easy to read. – hnefatl Nov 06 '17 at 19:58
  • 1
    This has possibly already been answered. https://stackoverflow.com/questions/6248044/c-comparing-two-vectors – Sailanarmo Nov 06 '17 at 19:59
  • 2
    don't see how you can do that efficiently without sorting... – OznOg Nov 06 '17 at 19:59
  • 1
    Do you need an efficient solution, or just a solution which works? – geza Nov 06 '17 at 20:02
  • 1
    @OznOg: Easy. Use a map, count the elements of the first vector, uncount the elements of the second. If there's a count in the map that's not 0, the vectors aren't equal. (There are a couple of optimizations you can do from there, but that's the basic algorithm.) – cHao Nov 06 '17 at 20:02
  • @OznOg: with hashtable, for example – geza Nov 06 '17 at 20:03
  • @OznOg you can convert one of the vectors to an `unordered_multiset` but sorting is generally easier. – Mark Ransom Nov 06 '17 at 20:03

1 Answers1

4

As duplicates shall not matter, you could simply insert the elements of the vectors in a separate std::set, respectively, and then compare the sets using operator ==. I think the use of sets expresses the intent of your program best:

int main() {

    vector<int> v1 =  { 1, 4, 9, 16, 9, 7, 4, 9, 11 };
    vector<int> v2 =  { 11, 11, 7, 9, 16, 4, 1 };

    set<int> s1;
    s1.insert(v1.begin(), v1.end());

    set<int> s2;
    s2.insert(v2.begin(), v2.end());

    bool isEqual = (s1 == s2);
    cout << "v1 and v2 are " << (isEqual ? "" : "not ") << "equal." << endl;

    return 0;

}
Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58
  • It is often more efficient to copy to a `vector` and use `sort` than it is to copy to a `set`. – Mark Ransom Nov 06 '17 at 21:23
  • 1
    @Mark Ransom: From the example given by OP, the problem is about comparing sets. So I preferred this solution over a more efficient but probably less intuitive solution. But of course, if it turns out that the use of sets will get to slow - and only a profiler will show that this gets a problem - one could think of a more efficient solution. – Stephan Lechner Nov 06 '17 at 21:30
  • That was a subtlety in the question that I hadn't noticed before. My method wouldn't work in that case, it doesn't eliminate duplicates. – Mark Ransom Nov 06 '17 at 21:48