- 对于内置类型,由于编译系统已经知道了,什么时候两个数据相等(如 10 == 10 为true),可直接使用std::find查找
std::vector<int> m_v;
std::vector<int>::iterator it = std::find(m_v.begin(), m_v.end(), 10);
- 对于结构体,需要重载运算符"=="
struct student
{
string name;
int score;
student()
: name("")
, score(0)
{
}
bool operator==(const student& s)
{
return name== s.name;
}
}
std::vector<student> m_v;
// some code
// 需要查找的对象
student s1;
s1.name = 'linduo'
std::vecot<student>::iterator = std::find(m_v.begin(), m_v.end(), s1);
if (it != m_v.end()) {
// find it
}
else {
// no find
}