我想得到两个向量对的交集。我可以使用使用默认比较器的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
吗?
答案 0 :(得分:2)
ranges::view::set_intersection
将结果作为新范围(在这种情况下为新视图)返回。请参见tests如何使用该功能。
示例:
using IntPair = std::pair<int,int>;
using IntPairVector = std::vector<IntPair>;
IntPairVector pathWire1 = {{1,1}, {2,2}, {3,3}};
IntPairVector pathWire2 = { {2,2}, {0,0}};
ranges::sort(pathWire1);
ranges::sort(pathWire2);
auto res = ranges::view::set_intersection(pathWire1, pathWire2);
for(auto&& p : res)
std::cout << p.first << ' ' << p.second << '\n';
// prints 2 2