set和map放自己的定义元素

set和map放入自己定义的元素,在声明时需要同时声明定义的<

 

#include <iostream>
#include<set>
#include<utility>
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() { set<chrom, mycmp> se; // 需要同时声明 chrom c = chrom(1,2); se.insert(c); for(auto& ele:se){ cout<<ele.obj.first<< " "<<ele.obj.second<<endl; } return 0; }

 

posted @ 2022-03-16 16:03  山谷清芳  阅读(46)  评论(0)    收藏  举报