dictionary - Comparison Operator for Structure key in C++ Map -
#include<bits/stdc++.h> using namespace std; struct segment{ int a; int b; int c; bool const operator<(const segment &o) const { return < o.a; } }; int main() { map<segment,int> mymap; map<segment,int>::iterator it; struct segment x,y,z; x.a=2; x.b=4; x.c=6; y.a=2; y.b=5; y.c=8; z.a=2; z.b=4; z.c=6; mymap[y]++; mymap[z]++; mymap[x]++; for( =mymap.begin(); != mymap.end(); it++) cout<<(*it).first.a<<" "<<(*it).second<<endl; return 0; }
it gives result as
2 3
but want print
2 1 2 2
in short want increment value of map if same struct instance fed instead of making new copy
imo best way compare multiple members using std::tie
harder mess up:
bool const operator<(const segment &o) const { return std::tie(a, b, c) < std::tie(o.a, o.b, o.c); }
edit: add link cppreference example there question.
Comments
Post a Comment