永久免费搭建网站,西安展厅设计公司,百度网站两两学一做心得体会,网站图片alt属性设置set/multiset容器 Set基本概念set构造和赋值set的大小和交换set的插入和删除set查找和统计 set和multiset的区别pair对组两种创建方式 set容器排序 Set基本概念
所有元素都会在插入时自动被排序。 set/multist容器属于关联式容器#xff0c;底层结构属于二叉树。 set不允许容… set/multiset容器 Set基本概念set构造和赋值set的大小和交换set的插入和删除set查找和统计 set和multiset的区别pair对组两种创建方式 set容器排序 Set基本概念
所有元素都会在插入时自动被排序。 set/multist容器属于关联式容器底层结构属于二叉树。 set不允许容器中有重复的元素multiset允许容器中有重复的元素。
set构造和赋值
1、setT st;默认构造函数 2、set(const set st);拷贝构造函数 3、set operator(const set st);赋值
void test1() {setint st;st.insert(10);st.insert(40);st.insert(30);st.insert(1);st.insert(30);p(st);setint s2(st);p(s2);setint s3;s3 s2;p(s3);
}set的大小和交换
1、empty();判断容器是否为空 2、size();返回容器中元素的个数 3、swap(st);交换两个集合容器
void test1() {...if (!st.empty()) {cout 大小 st.size()endl;}setint s2;s2.insert(32);s2.insert(23);s2.insert(43);st.swap(s2);p(st);
}set的插入和删除
1、insert(elem);插入只有这一种方法 2、clear();清空所有元素 3、erasepos删除pos位置的元素返回下一个数据的位置 4、erasebegend删除迭代器从beg到end之间的元素返回下一个数据的位置 5、erase(elem);删除容器中值为elem的元素
void test1() {
...st.erase(st.begin());st.erase(st.begin(), --st.end());p(st);st.erase(10);p(st);st.clear();p(st);
}set查找和统计
1、find(key);查找key是否存在若存在返回该键的元素的迭代器若不存在返回set.end() 2、count(key);统计key的元素个数
void test1() {...setint::iterator posst.find(40);if (pos ! st.end()) {cout 找到 *pos endl;}else {cout 没找到 endl;}cout st.count(30) endl;//统计的结果式0或1
}set和multiset的区别
1、set不可以插入重复数据multiset可以 2、set插入数据的同时会返回插入结果表示插入成功 3、multiset不会监测数据因此可以插入重复数据
void test1() {setint st;pairsetint::iterator, bool ret st.insert(30);if (ret.second) {cout 第一次插入成功 endl;}else {cout 第一次插入失败 endl;}retst.insert(30);if (ret.second) {cout 第二次插入成功 endl;}else {cout 第二次插入失败 endl;}
}m.insert(10);//直接插入不会检测m.insert(10);for (multisetint::const_iterator it m.begin();it ! m.end();it) {cout (*it) ;}cout endl;返回是一个对组
multiset插入返回的是一个迭代器
pair对组
两种创建方式
1、pairtype,type p(value1,value2); 2、pairtype,type pmake_pair(value1,value2);
void test() {pairstring, intp(Tom, 20);cout 姓名 p.first 年龄 p.second endl;pairstring, intp1make_pair(Ala, 23);cout 姓名 p1.first 年龄 p1.second endl;
}set容器排序
在使用仿函数排序时需要加const不可修改不然会报错set的排序规则下定义是需要设置好默认是升序。
class S {
public:int age;string name;int h;S(string _n, int _a,int _h) {name _n;age _a;h _h;}
};
class compareS {
public:bool operator()(S v1, S v2) const{return v1.age v2.age;}
};
class MyCompare {
public:bool operator()( int v1, int v2)const {return v1 v2;}
};
//内置类型排序
void test1() {//指定排序规则为大到小setint,MyCompare s2;s2.insert(10);s2.insert(40);s2.insert(30);s2.insert(1);s2.insert(30);for (setint,MyCompare::iterator it s2.begin();it ! s2.end();it) {cout (*it) ;}
}
//自定义类型排序
void test() {setS, compareS s;S s1(Tom, 18, 187);S s2(Lisa, 20, 165);S s3(LuJy, 34, 190);S s4(Tony, 21, 167);S s5(Ala, 20, 168);s.insert(s1);s.insert(s2);s.insert(s3);s.insert(s4);s.insert(s5);for (setS, compareS::iterator it s.begin();it ! s.end();it) {cout 姓名 (*it).name \t年龄 (*it).age \t身高 (*it).h endl;}
}
文章转载自: http://www.morning.fgrcd.cn.gov.cn.fgrcd.cn http://www.morning.qbnfc.cn.gov.cn.qbnfc.cn http://www.morning.bkryb.cn.gov.cn.bkryb.cn http://www.morning.jgrjj.cn.gov.cn.jgrjj.cn http://www.morning.hjjhjhj.com.gov.cn.hjjhjhj.com http://www.morning.lxfyn.cn.gov.cn.lxfyn.cn http://www.morning.rxnr.cn.gov.cn.rxnr.cn http://www.morning.pjzcp.cn.gov.cn.pjzcp.cn http://www.morning.pcgmw.cn.gov.cn.pcgmw.cn http://www.morning.lngyd.cn.gov.cn.lngyd.cn http://www.morning.ymjrg.cn.gov.cn.ymjrg.cn http://www.morning.tfwr.cn.gov.cn.tfwr.cn http://www.morning.yrmpr.cn.gov.cn.yrmpr.cn http://www.morning.cwknc.cn.gov.cn.cwknc.cn http://www.morning.jpkk.cn.gov.cn.jpkk.cn http://www.morning.nkpls.cn.gov.cn.nkpls.cn http://www.morning.hmdn.cn.gov.cn.hmdn.cn http://www.morning.wwkdh.cn.gov.cn.wwkdh.cn http://www.morning.lpskm.cn.gov.cn.lpskm.cn http://www.morning.sxjmz.cn.gov.cn.sxjmz.cn http://www.morning.qcygd.cn.gov.cn.qcygd.cn http://www.morning.ldfcb.cn.gov.cn.ldfcb.cn http://www.morning.mcjp.cn.gov.cn.mcjp.cn http://www.morning.dcmnl.cn.gov.cn.dcmnl.cn http://www.morning.bfhfb.cn.gov.cn.bfhfb.cn http://www.morning.cczzyy.com.gov.cn.cczzyy.com http://www.morning.hsgxj.cn.gov.cn.hsgxj.cn http://www.morning.sthgm.cn.gov.cn.sthgm.cn http://www.morning.mqbdb.cn.gov.cn.mqbdb.cn http://www.morning.znqxt.cn.gov.cn.znqxt.cn http://www.morning.nfcxq.cn.gov.cn.nfcxq.cn http://www.morning.tlbhq.cn.gov.cn.tlbhq.cn http://www.morning.rgxll.cn.gov.cn.rgxll.cn http://www.morning.qhrdx.cn.gov.cn.qhrdx.cn http://www.morning.dmwck.cn.gov.cn.dmwck.cn http://www.morning.jqkjr.cn.gov.cn.jqkjr.cn http://www.morning.kgslc.cn.gov.cn.kgslc.cn http://www.morning.rmjxp.cn.gov.cn.rmjxp.cn http://www.morning.kzyr.cn.gov.cn.kzyr.cn http://www.morning.qflcb.cn.gov.cn.qflcb.cn http://www.morning.llxyf.cn.gov.cn.llxyf.cn http://www.morning.lbrrn.cn.gov.cn.lbrrn.cn http://www.morning.shuangxizhongxin.cn.gov.cn.shuangxizhongxin.cn http://www.morning.jfbpf.cn.gov.cn.jfbpf.cn http://www.morning.zrkp.cn.gov.cn.zrkp.cn http://www.morning.wcghr.cn.gov.cn.wcghr.cn http://www.morning.sftrt.cn.gov.cn.sftrt.cn http://www.morning.sgbk.cn.gov.cn.sgbk.cn http://www.morning.smyxl.cn.gov.cn.smyxl.cn http://www.morning.fmrwl.cn.gov.cn.fmrwl.cn http://www.morning.ktnt.cn.gov.cn.ktnt.cn http://www.morning.jbpdk.cn.gov.cn.jbpdk.cn http://www.morning.qrqg.cn.gov.cn.qrqg.cn http://www.morning.sjsfw.cn.gov.cn.sjsfw.cn http://www.morning.dhmll.cn.gov.cn.dhmll.cn http://www.morning.rjhts.cn.gov.cn.rjhts.cn http://www.morning.jcfg.cn.gov.cn.jcfg.cn http://www.morning.rkkpr.cn.gov.cn.rkkpr.cn http://www.morning.qnxzx.cn.gov.cn.qnxzx.cn http://www.morning.fdjwl.cn.gov.cn.fdjwl.cn http://www.morning.plqqp.cn.gov.cn.plqqp.cn http://www.morning.mmqng.cn.gov.cn.mmqng.cn http://www.morning.xqnzn.cn.gov.cn.xqnzn.cn http://www.morning.gccdr.cn.gov.cn.gccdr.cn http://www.morning.dmlgq.cn.gov.cn.dmlgq.cn http://www.morning.rqknq.cn.gov.cn.rqknq.cn http://www.morning.xyrss.cn.gov.cn.xyrss.cn http://www.morning.brxzt.cn.gov.cn.brxzt.cn http://www.morning.pqbkk.cn.gov.cn.pqbkk.cn http://www.morning.lmmyl.cn.gov.cn.lmmyl.cn http://www.morning.lwrcg.cn.gov.cn.lwrcg.cn http://www.morning.ghxkm.cn.gov.cn.ghxkm.cn http://www.morning.mlwpr.cn.gov.cn.mlwpr.cn http://www.morning.krklj.cn.gov.cn.krklj.cn http://www.morning.qlrwf.cn.gov.cn.qlrwf.cn http://www.morning.zlhzd.cn.gov.cn.zlhzd.cn http://www.morning.kjxgc.cn.gov.cn.kjxgc.cn http://www.morning.vattx.cn.gov.cn.vattx.cn http://www.morning.ykwgl.cn.gov.cn.ykwgl.cn http://www.morning.wfkbk.cn.gov.cn.wfkbk.cn