ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

C++ //set/multiset 容器 //set不可以插入重复的数字 multiset可以插入重复的数字 //ste容器构造和赋值 //set大小和交换 //set 插入和删除 //set查找

2021-08-16 08:32:47  阅读:218  来源: 互联网

标签:insert set cout printSet s3 s4 插入 multiset


  1 //set/multiset 容器 //set不可以插入重复的数字 multiset可以插入重复的数字
  2 //ste容器构造和赋值  //set大小和交换 //set 插入和删除
  3 //set查找和统计        //set 和 multiset 区别  
  4 //pair 对组创建    //set存放自定义数据类型  //set内置数据 进行排序
  5 
  6 
  7 #include<iostream>
  8 #include<string>
  9 #include<set>
 10 
 11 using namespace std;
 12 void printSet(set<int>&s)
 13 {
 14     for (set<int>::iterator it = s.begin(); it != s.end(); it++)
 15     {
 16         cout << *it << " ";
 17     }
 18     cout << endl;
 19 }
 20 void test01()
 21 {
 22     set<int>s1;
 23     //插入数据 只有insert方式
 24     s1.insert(10);
 25     s1.insert(40);
 26     s1.insert(40);
 27     s1.insert(30);
 28     s1.insert(20);
 29 
 30     
 31     //遍历容器
 32     //set容器特点:所有元素插入时候会被排序
 33     //set容器不允许插入重复的元素
 34     printSet(s1);
 35 
 36 
 37     //拷贝构造
 38     set<int>s2(s1);
 39     printSet(s2);
 40 
 41     //赋值操作
 42     set<int>s3;
 43     s3 = s2;
 44     printSet(s3);
 45 
 46 }
 47 
 48 //set大小和交换
 49 void test02()
 50 {
 51     set<int>s2;
 52     s2.insert(10);
 53     s2.insert(20);
 54     s2.insert(30);
 55     s2.insert(40);
 56     s2.insert(40);
 57 
 58     printSet(s2);
 59     
 60     //是否为空
 61     if (s2.empty())
 62     {
 63         cout << "s2为空!" << endl;
 64     }
 65     else
 66     {
 67         cout << "s2不为空!" << endl;
 68         cout << "s2的个数为:" << s2.size() << endl;
 69     }
 70 
 71     set<int>s3;
 72     s3.insert(100);
 73     s3.insert(200);
 74     s3.insert(3000);
 75     s3.insert(400);
 76     s3.insert(401);
 77 
 78     cout << "交换前:" << endl;
 79     printSet(s2);
 80     printSet(s3);
 81     cout << "交换后:" << endl;
 82 
 83     s2.swap(s3);
 84     printSet(s2);
 85     printSet(s3);
 86     
 87 
 88 
 89 
 90 }
 91 
 92 
 93 //set 插入和删除
 94 void test03()
 95 {
 96     set<int>s3;
 97     s3.insert(100);
 98     s3.insert(200);
 99     s3.insert(3000);
100     s3.insert(400);
101     s3.insert(401);
102 
103     printSet(s3);
104 
105     //删除
106     s3.erase(s3.begin());
107     printSet(s3);
108 
109     //删除重载
110     s3.erase(200);
111     printSet(s3);
112 
113     //清空
114     s3.erase(s3.begin(), s3.end());
115     printSet(s3);
116 
117     s3.clear();
118     printSet(s3);
119 }
120 
121 //set查找和统计
122 void test04()
123 {
124     set<int>s4;
125     s4.insert(100);
126     s4.insert(200);
127     s4.insert(3000);
128     s4.insert(400);
129     s4.insert(401);
130     printSet(s4);
131 
132     set<int>::iterator pos = s4.find(20000);
133     
134     if (pos != s4.end())
135     {
136         cout << "找到了元素:" << *pos << endl;
137     }
138     else
139     {
140         cout << "没有找到!" << endl;
141     }
142      
143 
144 }
145 
146 //统计
147 void test05()
148 {
149     set<int>s4;
150     s4.insert(100);
151     s4.insert(200);
152     s4.insert(3000);
153     s4.insert(400);
154     s4.insert(401);
155     printSet(s4);
156 
157     //统计
158     int num = s4.count(20000);
159     //对于set而言 统计结果 1 or 0
160     cout << "num = " << num << endl;
161 
162 }
163 
164 //set 和 multiset 区别
165 
166 void test06()
167 {
168     set<int>s6;
169 
170     pair<set<int>::iterator, bool> ret = s6.insert(10);
171 
172     if (ret.second)
173     {
174         cout << "第一次插入成功!" << endl;
175     }
176     else
177     {
178         cout << "第一次插入失败!" << endl;
179     }
180     ret = s6.insert(10);
181     if (ret.second)
182     {
183         cout << "第二次插入成功!" << endl;
184     }
185     else
186     {
187         cout << "第二次插入失败!" << endl;
188     }
189 
190     multiset<int>m1;
191     //允许插入重复值
192     m1.insert(10);
193     m1.insert(10);
194     m1.insert(10);
195     m1.insert(10);
196     m1.insert(10);
197     m1.insert(10);
198 
199     for (multiset<int>::iterator it = m1.begin(); it != m1.end(); it++)
200     {
201         cout << *it << " ";
202     }
203     cout << endl;
204 
205 }  
206 
207 //pair 对组创建 pair<type,type> p (value1,value2) pair<type,type> 
208 //p = make_pair(value1,value2);
209 void test07()
210 {
211     //第一种
212     pair<string ,int>p("张三", 20);
213     cout << "姓名:" << p.first << " 年龄:" << p.second << endl;
214 
215     //第二种
216     pair<string, int>p2 = make_pair("lisi", 20);
217     cout << "姓名:" << p2.first << " 年龄:" << p2.second << endl;
218 }
219 //set容器排序 存放内置数据类型
220 //仿函数 从大到小
221 class  MyCompare
222 {
223 public:
224     bool operator()(int v1,int v2)const
225     {
226         return v1 > v2;
227     }
228 };
229 void test08()
230 {
231     set<int>s8;
232     s8.insert(20);
233     s8.insert(30);
234     s8.insert(10);
235     s8.insert(50);
236     s8.insert(40);
237 
238 
239     for (set<int>::iterator it = s8.begin(); it != s8.end(); it++)
240     {
241         cout << *it << " ";
242     }
243     cout << endl;
244 
245     //指定排序规则 从大到小
246     set<int,MyCompare>s9;
247     s9.insert(20);
248     s9.insert(30);
249     s9.insert(10);
250     s9.insert(50);
251     s9.insert(40);
252 
253     for (set<int,MyCompare>::iterator it = s9.begin(); it != s9.end(); it++)
254     {
255         cout << *it << " ";
256     }
257     cout << endl;
258 }
259 
260 //set排序存放自定义数据类型
261 class Person
262 {
263 public:
264     Person(string name, int age)
265     {
266         this->m_Age = age;
267         this->m_Name = name;
268     }
269 
270     string m_Name;
271     int m_Age;
272 };
273 
274 //仿函数
275 class MyComparePerson
276 {
277 public:
278     bool operator()(const Person&p1,const Person&p2)const
279     {
280         //按照年龄 降序
281         return p1.m_Age > p2.m_Age;
282     }
283 };
284 void test09()
285 {
286     //自定义的数据类型 都会指定排序规则
287     set<Person, MyComparePerson>s;
288 
289     //创建Person对象
290     Person p1("刘备", 25);
291     Person p2("关羽", 300);
292     Person p3("张飞", 78);
293     Person p4("曹操", 20);
294 
295     s.insert(p1);
296     s.insert(p2);
297     s.insert(p3);
298     s.insert(p4);
299 
300     for (set<Person, MyComparePerson>::iterator it = s.begin(); it != s.end(); it++)
301     {
302         cout << "姓名:" << it->m_Name << "\t年龄: " << it->m_Age << endl;
303     }
304     
305 }
306 
307 
308 int main()
309 {
310     test01();
311 
312 
313     test02();
314     test03();
315     test04();
316     test05();
317 
318     test06();
319     test07();
320      
321      test08();
322 
323     test09();
324     system("pause");
325     return 0;
326 }

 

标签:insert,set,cout,printSet,s3,s4,插入,multiset
来源: https://www.cnblogs.com/Bytezero/p/15145966.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有