c++ - error while performing set_intersection with a map containing key and a structure -
i want find intersection between 2 maps. map has structure map<int,line>
, line
structure. problem when use set_intersection
performing intersection following error represented in image.
below code
#include <string> #include <iostream> #include <map> #include <algorithm> using namespace std; typedef pair<int, int> point; struct line { int lineid; point starting, ending; }; int main(int argc, char* argv[]) { typedef map<int, line> mymap; line w,x,y,z; w.lineid = 1; w.starting = { 5, 1 }; w.ending = { 5, 100 }; x.lineid = 2; x.starting = { 20, 56 }; x.ending = { 120, 56 }; y.lineid = 3; y.starting = { 100, 150 }; y.ending = { 100, 200 }; z.lineid = 4; z.starting = { 330, 50 }; z.ending = { 330, 150 }; mymap bin1; bin1.insert({ w.lineid, w }); bin1.insert({ x.lineid, x }); bin1.insert({ y.lineid, y }); mymap bin2; bin2.insert({ x.lineid, x }); bin2.insert({ y.lineid, y }); bin2.insert({ z.lineid, z }); mymap out; mymap::iterator out_itr(out.begin()); set_intersection(bin1.begin(), bin1.end(), bin2.begin(), bin2.end(), inserter(out, out_itr)); cout << "" << out.size(); return 0; }
any solve issue helpful.
the compiler complaining not finding operator<
compare lines object (you need compare std::pair<int, line>
, , comparator needs compare lines).
you must provide comparator lines :
example :
bool operator<(const line& other) const { return other.lineid < lineid; // or whatever logic want compare lines. }
live example here
note :
alternatively, can directly provide comparator set_intersection
, e.g. here lambda :
set_intersection(bin1.begin(), bin1.end(), bin2.begin(), bin2.end(), inserter(out, out_itr), [] (const std::pair<int, line>& p1, const std::pair<int, line>& p2) { return p1.first != p2.first; } );
Comments
Post a Comment