【发布时间】:2019-12-03 18:06:01
【问题描述】:
我想得到两个成对向量的交集。我可以使用使用默认比较器的 stl 来做到这一点(? - 如果我说错了,请纠正我)。
std::vector<std::pair<int, int>> pathWire1 = {{1,1}, {2,2}, {3,3}};
std::vector<std::pair<int, int>> pathWire2 = { {2,2}, {0,0}};
std::vector<std::pair<int,int>> res;
std::sort(pathWire1.begin(), pathWire1.end());
std::sort(pathWire2.begin(), pathWire2.end());
std::set_intersection(pathWire1.begin(), pathWire1.end(),
pathWire2.begin(), pathWire2.end(),
std::back_inserter(res));
//res contains {2,2}
充其量我想用范围来做:
ranges::sort(pathWire1);
ranges::sort(pathWire2);
ranges::view::set_intersection(pathWire1, pathWire2);
//ranges::view::set_intersection(pathWire1, pathWire2, std::back_inserter(res);
我尝试关注这个documentation,但是我无法编译它,因为模板无法专门化:
error C2672: 'operator __surrogate_func': no matching overloaded function found
我需要提供自定义比较器吗?我在编译ranges::sort 时也遇到了问题。是不是因为std::vector 专门用于非平凡类型std::pair?
【问题讨论】:
-
如果你想要急切的评估,不要使用视图:wandbox.org/permlink/36hqDjXRU1dp4kuS