开发者

compare vector with other kind of container in stl? [closed]

开发者 https://www.devze.com 2022-12-28 09:54 出处:网络
Closed. This question needs details or clarity. It is not currently accepting answers. 开发者_运维技巧
Closed. This question needs details or clarity. It is not currently accepting answers. 开发者_运维技巧

Want to improve this question? Add details and clarify the problem by editing this post.

Closed 7 years ago.

Improve this question

compare 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号