获取 range-v3 向量对的交集
Get range-v3 intersection of vector of pairs
我想得到两个向量对的交集。我可以使用使用默认比较器的 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
吗?
ranges::view::set_intersection
returns 结果作为新范围(在本例中为新视图)。请参阅 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
LIVE DEMO
我想得到两个向量对的交集。我可以使用使用默认比较器的 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
吗?
ranges::view::set_intersection
returns 结果作为新范围(在本例中为新视图)。请参阅 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