簡體   English   中英

獲取對向量的 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);

我試圖按照這個文檔但是我無法編譯它,因為模板沒有專門化:

error C2672: 'operator __surrogate_func': no matching overloaded function found

我需要提供自定義比較器嗎? 我在編譯ranges::sort也遇到了問題。 是因為std::vector專門用於非平凡類型std::pair嗎?

ranges::view::set_intersection將結果作為新范圍(在本例中為新視圖)返回。 請參閱測試如何使用該功能。

例子:

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

現場演示

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM