检查一个std :: vector是否包含某个对象? [英] check if a std::vector contains a certain object?
本文介绍了检查一个std :: vector是否包含某个对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能重复:
如何在std :: vector中找到一个项目?
在algorithm.h中有一些东西,它允许你检查一个std :: container是否包含一些东西?或者使用一种方法:
Is there something in algorithm.h which allows you to check if a std:: container contains something? Or a way to make one ex:
if(a.x == b.x && a.y == b.y)
return true;
return false;
这只能使用std :: map才能完成,因为它使用键?
can this only be done with std::map since it uses keys?
感谢
推荐答案
检查 v
包含 x
:
#include <algorithm>
if(std::find(v.begin(), v.end(), x) != v.end()) {
/* v contains x */
} else {
/* v does not contain x */
}
检查 v
是否包含元素(非空):
Checking if v
contains elements (is non-empty):
if(!v.empty()){
/* v is non-empty */
} else {
/* v is empty */
}
这篇关于检查一个std :: vector是否包含某个对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文