所要做的就是检查vector中是否存在一个元素,这样就可以处理每个case。
if ( item_present )
do_this();
else
do_that();"
正如其他人所说,使用STL find或find_if函数。但是,如果你在非常大的矢量搜索,这会影响性能,您可能要排序的载体,然后使用binary_search,lower_bound或upper_bound算法。
你可以使用std::find从<algorithm>:
std::find(vector.begin(), vector.end(), item) != vector.end()
这将返回一个布尔(true如果存在,false否则)。用你的例子:
#include <algorithm>
if ( std::find(vector.begin(), vector.end(), item) != vector.end() )
do_this();
else
do_that();