how to control if two vectors have the same values

Hi everybody's here,
i need to write a function to control if two vectors have the same values and the same count for each value.

Any hint for me?
Thanks for all
How about you elaborate more on the word "control"?

If you want to check whether 2 vectors are equal, just use the == operator:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <vector>
#include <iostream>

using namespace std;

int main()
{
   vector<double> v1,v2;
   
   v1.push_back(5);
   v1.push_back(8);
   v2.push_back(5);
   v2.push_back(8);
   
   if(v1 == v2)
      cout<<"Vectors are equal"<<endl;
}
Last edited on
Thanks, but does operator == work if elements in the two vectors are not in the same order?
They have to be exactly the same for the == to return true; i.e., size, order, values.
It's my problem, I need that returns true also if the elements are not in order
If you don't care about the order you can sort the two vectors before comparing them.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <vector>
#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
   vector<double> v1,v2;
   
   v1.push_back(5);
   v1.push_back(8);
   v2.push_back(5);
   v2.push_back(8);
   
   std::sort(v1.begin(), v1.end());
   std::sort(v2.begin(), v2.end());
   
   if(v1 == v2)
      cout<<"Vectors are equal"<<endl;
}
Topic archived. No new replies allowed.