I wrote the following code in order to check if at least one element in vector is not in another vector.
There are no duplicates in the vectors. Only unique elements
Is there a more elegant way to do it by using the stl?
// Online C++ compiler to run C++ program online #include <iostream> #include <vector> #include <algorithm> using namespace std; bool areVectorsDifferent(vector<int> &a, vector<int> &b){ if(a.size() != b.size()){ return true; } std::sort(a.begin(),a.end()); std::sort(b.begin(),b.end()); for(int i = 0; i < a.size();i++){ if(a[i] != b[i]) { return true; } } return false; } int main() { bool isDifferent = false; vector<int> a = {1,2,3,5}; vector<int> b = {4,3,2,1}; std::cout << areVectorsDifferent(a,b) << std::endl; return 0; }
return a != b;is a bit nicer than your loop.{1,2,3,5,5};and{1,1,2,3,5};for example.