set和map放自己的定义元素
set和map放入自己定义的元素,在声明时需要同时声明定义的<
#include#include<set> #include using namespace std;
// 自定义元素 struct chrom{ chrom(int a,int b){obj.first=a; obj.second=b;}; pair<int, int> obj; };
struct mycmp { bool operator()(const chrom& c1, const chrom& c2) const { if (c1.obj.first < c2.obj.first) return true; else return false; } }; int main() { setse; // 需要同时声明 chrom c = chrom(1,2); se.insert(c); for(auto& ele:se){ cout< " "< endl; } return 0; }