Use the std::equal
function from the <algorithm>
header:
if (std::equal(v1.begin(), v1.begin() + n, v2.begin())
std::cout << "success" << std::endl;
Note that both vectors must have at least n
elements in them. If either one is too short, behavior of your program will be undefined.
If you want to check whether the entire vector is equal to the other, just compare them like you'd compare anything else:
if (v1 == v2)
Your (failed) code was comparing an iterator of one vector with an iterator of the other. Iterators of equal vectors are not equal. Each iterator is tied to the sequence it's iterating, so an iterator from one vector will never be equal to the iterator of another.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…