介绍
排序重载 <
查找重载 ()
Demo
struct XXX
{
int a;
int b;
bool operator<(const struct XXX& xxx) const
{
return this->a < xxx.a;
}
bool operator()(const struct XXX& xxx)const
{
if (this->b == xxx.b)
{
return true;
}
return false;
}
};
void t()
{
std::multiset<XXX> s;
XXX xxx;
xxx.a = 3;
xxx.b = 5;
s.insert(xxx);
xxx.b = 0;
xxx.a = 1;
s.insert(xxx);
xxx.a = 2;
s.insert(xxx);
for (auto it : s)
{
std::cout << it.a << " ";
}
xxx.b = 5;
auto it = std::find_if(s.begin(), s.end(), xxx);
if (it == s.end())
{
std::cout << "not found" << std::endl;
}
else
{
std::cout << "found: " << it->a << " -> " << it->b << std::endl;
}
}
map同样适用,只不过形参要加上pair