You can use std::find
from <algorithm>
:
(您可以从<algorithm>
使用std::find
:)
#include <vector>
vector<int> vec;
//can have other data types instead of int but must same datatype as item
std::find(vec.begin(), vec.end(), item) != vec.end()
This returns a bool ( true
if present, false
otherwise).
(这将返回一个布尔值(如果存在则为true
,否则为false
)。)
With your example: (以您的示例为例:)
#include <algorithm>
#include <vector>
if ( std::find(vec.begin(), vec.end(), item) != vec.end() )
do_this();
else
do_that();
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…