compare vector with other kind of container in stl? [closed]
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this questioncompare vector with other kind of container in stl?
You can use std::equal()
from <algorithm>
.
Something like:
if ((vect.size() == otherContainer.size()) &&
std::equal( vect.begin(), vect.end(), otherContainer.begin()) {
// ...
}
Note that if the other container doesn't have enough elements std::equal()
won't work (undefined behavior), hence the check for size()
which you may or may not really need if you already know that there are enough elements in the other container.
Note that the other answers will allow you to compare for equality, but if you want a "character-by-character" compare, you should use std::lexographical_compare
.
You can use std::equal algorithm to do this.
A vector implements a dynamic resizable array, a list implements a linked list, and a deque implements something like a hybrid of those. Containers like set, map, multiset and multimap are associative. Besides those, there are container adaptors like queue, priority_queue and stack. A vector can be used as an underlying type of the last two.
精彩评论