Конечно!
Вы можете написать функтор для поиска элементов множества, где ЛИБО х или у 4, и стереть такие элементы.Вам нужно, чтобы boost::bind
упростила привязку:
#include <set>
#include <algorithm>
#include <boost/bind.hpp>
struct Foo {
int x, y;
bool operator<(const Foo& rhs) const {
return (x < rhs.x || (x == rhs.x && y < rhs.y));
}
bool hasValue(int v) const {
return (x == v || y == v);
}
};
int main()
{
std::set<Foo> s;
Foo a = {4,2}; s.insert(a);
Foo b = {3,4}; s.insert(b);
std::cout << s.size() << std::endl; // will output: "2"
std::set<Foo>::iterator it;
// Search for an element with the given value '4' in either x or y
// Erase first matching element found, if any
// (our first element, which has x=4)
it = std::find_if(s.begin(), s.end(), boost::bind(&Foo::hasValue, _1, 4));
if (it != s.end())
s.erase(it);
std::cout << s.size() << std::endl; // will output: "1"
// Erase the next one
// (our second element, which has y=4)
it = std::find_if(s.begin(), s.end(), boost::bind(&Foo::hasValue, _1, 4));
if (it != s.end())
s.erase(it);
std::cout << s.size() << std::endl; // will output: "0"
}
Не был тщательно протестирован на опечатки.
Вы можете получить те же функции без повышения, используя статическую функциюкоторый принимает Foo*
в качестве аргумента.