我給出了兩個按某些條件排序的容器(例如,按照其ID號排序的人員)。與`std :: set_intersection`相似,但產生相同元素對
我希望通過相同的標準找到兩個容器共有的所有元素,並且我希望訪問被認爲相等的兩個元素。
我想過執行合併,然後手動掃描相同的相鄰元素。但也許有一個更優雅的算法?
struct Employee
{
int id;
int salary;
Employee(int id, int salary);
};
struct ById
{
bool operator()(const Employee& left, const Employee& right) {
return left.id < right.id;
}
};
std::vector<Employee> first = { Employee(10, 1000),
Employee(12, 1000),
Employee(31, 10000) }; // Note: sorted by Id
std::vector<Employee> second = { Employee(1, 1500),
Employee(10, 2000),
Employee(31, 12000) }; // Note: sorted by Id
// prints the following:
// [id: 10, salary: 1000], [id: 10, salary: 2000] // id 10 exists in both containers
// [id: 31, salary: 10000], [id: 31, salary: 12000] // id 31 exists in both containers
MySetIntersection(// <--- I WANT TO KNOW HOW TO IMPLEMENT THIS
begin(first), end(first),
begin(second), end(second),
ById(),
[] (const Person& left, const Person& right) {
std::cout << "[" << left << "], [" << right << "]" std::endl;
}
);
不應該最後一行是'func(* first1 ++,* first2 ++)'? – Massa
是的,我在最後一分鐘重新安排了一些東西,並發現了一個錯誤。 – jhoffman0x
我看到你詳細闡述了你的回答,所以不幸的是我被接受了。 +1給你。 – jhoffman0x