93

Is there any way to compare two vectors?

if (vector1 == vector2)
    DoSomething();

Note: Currently, these vectors are not sorted and contain integer values.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
Jame
  • 21,150
  • 37
  • 80
  • 107
  • 26
    You mean you want to compare *irrespective* of the order of the elements in the vector? Otherwise, the above code should work. – Naveen Jun 06 '11 at 05:19
  • 1
    A simple way to compare if you have two unsorted arrays which you want to check contain exactly the same values, is to **sort them**, and then use any one of the standard library methods to do a comparison. –  Jun 06 '11 at 05:36
  • If only it were this simple. VS2013 uses `std::equals` to implement `==`, then asserts at runtime because the iterators don't come from the same container(!) You have to set `_ITERATOR_DEBUG_LEVEL=1` (or `=0`) on all the projects that use, or might use, `==`. Arg!! – Cameron Sep 23 '14 at 22:35
  • http://www.cplusplus.com/reference/vector/vector/operators `vector` supports both `==` and `<` type comparisons – Andrew Sep 14 '20 at 04:46

5 Answers5

95

Your code (vector1 == vector2) is correct C++ syntax. There is an == operator for vectors.

If you want to compare short vector with a portion of a longer vector, you can use theequal() operator for vectors. (documentation here)

Here's an example:

using namespace std;

if( equal(vector1.begin(), vector1.end(), vector2.begin()) )
    DoSomething();
solvingPuzzles
  • 8,541
  • 16
  • 69
  • 112
  • 4
    std::equal() is also valid for plain arrays, while equality operator is not, for example: int a[1000], b[1000]; if(std::equal(a, a+1000,b)) DoSomething(); – Mohamed El-Nakeep Apr 26 '14 at 05:19
  • 10
    Good answer, except that it encourages bad practice (`using namespace std`) instead of using the more clear `std::equal`. – Fritz Nov 28 '18 at 13:52
19

Check std::mismatch method of C++.

comparing vectors has been discussed on DaniWeb forum and also answered.

C++: Comparing two vectors

Check the below SO post. will helpful for you. they have achieved the same with different-2 method.

Compare two vectors C++

Community
  • 1
  • 1
Jhaliya - Praveen Sharma
  • 31,697
  • 9
  • 72
  • 76
  • Does that require the range to be sorted? – Naveen Jun 06 '11 at 05:21
  • 2
    `lexicographical_compare` works here too, and eliminates having to deal with which range is the longer range. – Billy ONeal Jun 06 '11 at 05:23
  • 16
    Please post the details of the link you ar reffering here, if that link goes down in future, the whole answer becomes useless. – Alok Save Jun 06 '11 at 05:24
  • 45
    Bad answer. You should provide code sample not just links - what if they get down? – c0dehunter Mar 07 '13 at 18:37
  • Note that this has no effect on unordered vectors with arbitrary item order: https://coliru.stacked-crooked.com/a/b66b432234265b7e https://coliru.stacked-crooked.com/a/66300e8d00a16abf – Zoe May 28 '20 at 15:22
8

C++11 standard on == for std::vector

Others have mentioned that operator== does compare vector contents and works, but here is a quote from the C++11 N3337 standard draft which I believe implies that.

We first look at Chapter 23.2.1 "General container requirements", which documents things that must be valid for all containers, including therefore std::vector.

That section Table 96 "Container requirements" which contains an entry:

Expression   Operational semantics
===========  ======================
a == b       distance(a.begin(), a.end()) == distance(b.begin(), b.end()) &&
             equal(a.begin(), a.end(), b.begin())

The distance part of the semantics means that the size of both containers are the same, but stated in a generalized iterator friendly way for non random access addressable containers. distance() is defined at 24.4.4 "Iterator operations".

Then the key question is what does equal() mean. At the end of the table we see:

Notes: the algorithm equal() is defined in Clause 25.

and in section 25.2.11 "Equal" we find its definition:

template<class InputIterator1, class InputIterator2>
bool equal(InputIterator1 first1, InputIterator1 last1,
           InputIterator2 first2);

template<class InputIterator1, class InputIterator2,
class BinaryPredicate>
bool equal(InputIterator1 first1, InputIterator1 last1,
           InputIterator2 first2, BinaryPredicate pred);

1 Returns: true if for every iterator i in the range [first1,last1) the following corresponding conditions hold: *i == *(first2 + (i - first1)), pred(*i, *(first2 + (i - first1))) != false. Otherwise, returns false.

In our case, we care about the overloaded version without BinaryPredicate version, which corresponds to the first pseudo code definition *i == *(first2 + (i - first1)), which we see is just an iterator-friendly definition of "all iterated items are the same".

Similar questions for other containers:

Community
  • 1
  • 1
Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
4

According to the discussion here you can directly compare two vectors using

==

if (vector1 == vector2){
   //true
}
else{
   //false
}
Shahrukh Haider
  • 454
  • 2
  • 16
1

If they really absolutely have to remain unsorted (which they really don't.. and if you're dealing with hundreds of thousands of elements then I have to ask why you would be comparing vectors like this), you can hack together a compare method which works with unsorted arrays.

The only way I though of to do that was to create a temporary vector3 and pretend to do a set_intersection by adding all elements of vector1 to it, then doing a search for each individual element of vector2 in vector3 and removing it if found. I know that sounds terrible, but that's why I'm not writing any C++ standard libraries anytime soon.

Really, though, just sort them first.

  • or may be create two `set` objects and compare them? Wouldn't that be easier if it is really required not to touch the vectors. – Naveen Jun 06 '11 at 06:34
  • Depends how quick N lookup-and-remove operations are on a vector (`N*O(N)`) versus 2 set constructors sorting N elements (cplusplus.com's reference says "For unsorted sequences, linearithmic (N*logN)..") + a sorted comparison (`O(N)`). –  Jun 06 '11 at 06:55