### Compare 2 vectors

How do you compare 2 vectors with the same length?
Example:
 ``1234`` ``````vector i1 = {1, 2, 3, 4, 5}, i2 = {1, 3, 2, 5, 4}, i3 = {1, 2, 3, 4, 5};``````

 ``` i1 == i2 -> false i1 == i3 -> true```
http://www.cplusplus.com/reference/vector/vector/operators/

 ``123456789`` `````` vector i1 = {1, 2, 3, 4, 5}, i2 = {1, 3, 2, 5, 4}, i3 = {1, 2, 3, 4, 5}; cout << boolalpha; cout << "i1 == i2 " << (i1 == i2) << endl; cout << "i1 == i3 " << (i1 == i3) << endl;`````` ```i1 == i2 false i1 == i3 true```
Last edited on
What!!! Dang it!!!
Thanks anyway!
Yeah, dang it.
Hi,

Function to compare two vectors for equality.

template<typename T>
bool AreVectorsSame(const std::vector<T>& i_Vec1,const std::vector<T>& i_Vec2)
{
if(i_Vec1.size()!=i_Vec2.size())
return false;

for(size_t i=0;i<i_Vec1.size();i++)
if(i_Vec1[i]!=i_Vec2[i])
return false;

return true;
}

int _tmain(int argc, _TCHAR* argv[])
{
std::vector<int>
i1 = { 1, 2, 3, 4, 5 },
i2 = { 1, 3, 2, 5, 4 },
i3 = { 1, 2, 3, 4, 5 };

bool areSame1 = AreVectorsSame(i1, i2);//returns false
bool areSame2 = AreVectorsSame(i1, i3);//returns true

return 0;

}
Last edited on
Topic archived. No new replies allowed.