4

I have a problem while comparing the values in two vectors.

Following is the sample code of my program:

  template <typename T> bool CompareVectors(std::vector<T> vector1, std::vector<T> vector2)
  {
    std::sort(vector1.begin(),vector1.end());
    std::sort(vector2.begin(),vector2.end());
    if (vector1.size() != vector2.size())
      return false;
    else
    {
      bool found = false;
      std::vector<T>::iterator it;
      std::vector<T>::iterator it2;
      for (it = vector1.begin();it != vector1.end(); it++)
      {      
        for(it2 = vector2.begin(); it2 != vector2.end(); it2++)
        {
          if(it == it2) // here i have to check the values in the itearators are equal.
          {
            found = true;
            break;
          }
        }
        if(!found)
          return false;
        else
          found = false;
      }
      return true;  
    }
    };

In this sample code I have to compare the two vectors. For that I have sorted the two vectors using std::sort(). Since the data type in the vector is a template (I am using a class object in the vector), the std::sort() is not working properly. Ie, sometimes the two vectors give different order of elements after sorting.

So I am not able to use the std::equal() function also.

For an alternative solution, I have used two iterators for the twi vectors.

And iterating one vector and searches that element in the other vector. For this the iterator comparison is cannot be usable.

3
  • How did you implement operator< for the sorting ? It is probably your problem ... My bet is you have a vector of pointers and that your items get sorted by their address instead of their values. Commented Mar 12, 2012 at 4:55
  • Have you defined the < and == operator for the class that you're using? Commented Mar 12, 2012 at 4:56
  • yaa i have defined== , < , != operators for the class that i am using Yea... i am using poinetr element in the vectors for comparing.So thats the problem with std::sorting. So it is sorting with address. Commented Mar 12, 2012 at 5:03

3 Answers 3

3

First you've to use typename keyword here:

typename std::vector<T>::iterator it;
typename std::vector<T>::iterator it2;

without typename your code wouldn't even compile.

To compare the values pointed to by iterators, you've to do this:

if( *it == *it2)

You could write you compare function as:

//changed the name from CompareVectors() to equal()
template <typename T> 
bool equal(std::vector<T> v1, std::vector<T> v2)
{
  std::sort(v1.begin(),v1.end());
  std::sort(v2.begin(),v2.end());
  if ( v1.size() != v2.size() )
       return false;
  return std::equal(v1.begin(),v1.end(), v2.begin());
};
Sign up to request clarification or add additional context in comments.

2 Comments

Instead of testing the size and then calling std::equal you can simply say return v1 == v2;
@Blastfurnace: Thats good. I didn't know that there exists == non-member function to test equality of two vectors.
1

Should this line:

if(it == it2)

be

if (*it == *it2)

The first line is comparing pointers not the values.

Comments

0

There are multiple issues here. First, you say that std::sort() doesn't work. Have you overloaded the operator< for your class?

Also, you need to compare what the iterators are pointing to:

*it == *it2

Further, you need to iterate through both arrays at the same time (just one loop):

for (it = vector1.begin(), it2 = vector2.begin();
     it != vector1.end(), it2 != vector2.end();
     it++, it2++) {
  ...
}

Though really, you should just use std::equal() by overloading the operator==.

And from an efficiency standpoint, you should compare the size() values before you bother sorting the arrays.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.