template <typename T>class RuntimeCmp{public:enum cmp_mode {normal,reverse};private:cmp_mode mode;public:RuntimeCmp(cmp_mode m = normal) : mode(m){}bool operator() (const T& t1,const T& t2) const{return mode == normal ? t1<t2 : t2<t1;}bool operator==(const RuntimeCmp& rc){return mode == rc.mode;}};typedef set<int,RuntimeCmp<int> >intSet;void fill(intSet& set){set.insert(4);set.insert(7);set.insert(5);set.insert(1);set.insert(6);set.insert(2);set.insert(5);}void pr(intSet& is,string name){std::cout<<name;copy(is.begin(),is.end(),ostream_iterator<int>(cout," "));cout<<endl;}//使用pr时,只带一个参数intSet& is,然后通过is的参数名来输出比如:intSet coll1;fill(coll1);pr(coll1);直接就能输出coll1: 1,2,4,5,6,7而不是pr(coll1,"coll1: ")这样的。
添加回答
举报
0/150
提交
取消