当前位置:网站首页>Chapter 13 set/ multiset of STL
Chapter 13 set/ multiset of STL
2022-07-19 09:33:00 【qq_ forty-three million two hundred and five thousand two hundr】
1 set Basic concepts
2 set Construction and assignment
#include <set>
void printSet(set<int> & s)
{
for (set<int>::iterator it = s.begin(); it != s.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
// Construction and assignment
void test01()
{
set<int> s1;
s1.insert(20);
s1.insert(10);
s1.insert(30);
s1.insert(40);
printSet(s1);
// Copy structure
set<int>s2(s1);
printSet(s2);
// assignment
set<int>s3;
s3 = s2;
printSet(s3);
}
int main()
{
test01();
return 0;
}Output results :
10 20 30 40
10 20 30 40
10 20 30 403 set Size and swap
#include <set>
void printSet(set<int> & s)
{
for (set<int>::iterator it = s.begin(); it != s.end(); it++)
{
cout << *it << " "; }cout << endl;
}
// size
void test01()
{
set<int> s1;
s1.insert(10);
s1.insert(30);
s1.insert(20);
s1.insert(40);
if (s1.empty())
{
cout << "s1 It's empty " << endl;
}
else
{
cout << "s1 Not empty " << endl;
cout << "s1 The size is : " << s1.size() << endl;
}
}
// In exchange for
void test02()
{
set<int> s1;
s1.insert(10);
s1.insert(30);
s1.insert(20);
s1.insert(40);
set<int> s2;
s2.insert(100);
s2.insert(300);
s2.insert(200);
s2.insert(400);
cout << " Exchange before " << endl;
printSet(s1);
printSet(s2);
cout << endl;
cout << " After exchanging " << endl;
s1.swap(s2);
printSet(s1);
printSet(s2);
}
int main()
{
test01();
test02();
return 0;
}Output results :
s1 Not empty
s1 The size is : 4
Exchange before
10 20 30 40
100 200 300 400
After exchanging
100 200 300 400
10 20 30 404 set Insert and delete
#include <set>
void printSet(set<int> & s)
{
for (set<int>::iterator it = s.begin(); it != s.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
// Insert and delete
void test01()
{
set<int> s1;
// Insert
s1.insert(10);
s1.insert(30);
s1.insert(20);
s1.insert(40);
printSet(s1);
// Delete
s1.erase(s1.begin());
printSet(s1);
s1.erase(30);
printSet(s1);
// Empty
s1.erase(s1.begin(), s1.end());
printSet(s1);
s1.clear();
printSet(s1);
}
int main()
{
test01();
return 0;
}Output results :
10 20 30 40
20 30 40
20 405 set Find and count
#include <set>
// Find and count
void test01()
{
set<int> s1;
// Insert
s1.insert(10);
s1.insert(30);
s1.insert(20);
s1.insert(40);
// lookup
set<int>::iterator pos = s1.find(30);
if (pos != s1.end())
{
cout << " Found the element : " << *pos << endl;
}
else
{
cout << " Element not found " << endl;
}
// Statistics
int num = s1.count(30);
cout << "num = " << num << endl;
}
int main()
{
test01();
return 0;
}Output results :
Found the element : 30
num = 16 set and multiset difference
#include <set>
//set and multiset difference
void test01()
{
set<int> s;
pair<set<int>::iterator, bool> ret = s.insert(10);
if (ret.second)
{
cout << " The first insertion was successful !" << endl;
}
else
{
cout << " The first insertion failed !" << endl;
}
ret = s.insert(10);
if (ret.second)
{
cout << " The second insertion was successful !" << endl;
}
else
{
cout << " The second insertion failed !" << endl;
}
//multiset
multiset<int> ms;
ms.insert(10);
ms.insert(10);
for (multiset<int>::iterator it = ms.begin(); it != ms.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
int main()
{
test01();
return 0;
}Output results :
The first insertion was successful !
The second insertion failed !
10 107 pair Create for group
#include <string>
// Create for group
void test01()
{
pair<string, int> p(string("Tom"), 20);
cout << " full name : " << p.first << " Age : " << p.second << endl;
pair<string, int> p2 = make_pair("Jerry", 10);
cout << " full name : " << p2.first << " Age : " << p2.second << endl;
}
int main()
{
test01();
return 0;
}Output results :
full name : Tom Age : 20
full name : Jerry Age : 10summary :
8 set Container sorting
#include <set>
class MyCompare
{
public:
bool operator()(int v1, int v2)const
{
return v1 > v2;
}
};
void test01()
{
set<int> s1;
s1.insert(10);
s1.insert(40);
s1.insert(20);
s1.insert(30);
s1.insert(50);
// Default from small to large
for (set<int>::iterator it = s1.begin(); it != s1.end(); it++)
{
cout << *it << " ";
}
cout << endl;
// Specify collation
set<int, MyCompare> s2;
s2.insert(10);
s2.insert(40);
s2.insert(20);
s2.insert(30);
s2.insert(50);
for (set<int, MyCompare>::iterator it = s2.begin(); it != s2.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
int main()
{
test01();
return 0;
}
#include <string>
class Person
{
public:
Person(string name, int age)
{
this->m_Name = name;
this->m_Age = age;
}
string m_Name;
int m_Age;
};
class comparePerson
{
public:
bool operator()(const Person& p1, const Person& p2)const
{
// Sort by age Descending
return p1.m_Age > p2.m_Age;
}
};
void test01()
{
set<Person, comparePerson> s;
Person p1(" Liu bei ", 23);
Person p2(" Guan yu ", 27);
Person p3(" Zhang Fei ", 25);
Person p4(" zhaoyun ", 21);
s.insert(p1);
s.insert(p2);
s.insert(p3);
s.insert(p4);
for (set<Person, comparePerson>::iterator it = s.begin(); it != s.end(); it++)
{
cout << " full name : " << it->m_Name << " Age : " << it->m_Age << endl;
}
}
int main()
{
test01();
return 0;
}
Output results :
full name : Guan yu Age : 27
full name : Zhang Fei Age : 25
full name : Liu bei Age : 23
full name : zhaoyun Age : 21边栏推荐
- 代码庆端午--粽你心意
- Log desensitization - Reference
- [performance optimization methodology series] VI. summary
- 如何快速计算生成模型的FID、IS、sFID、Precision、Recall等关键评价指标?
- C# 读写文本,生成二维码
- EBay searches eBay products by keyword API return value description
- Uniapp warehouse management system source code
- 第一部分—C语言基础篇_2. 数据类型
- 【性能优化方法论系列】六、总结
- C language compilation process
猜你喜欢

uniapp仓库管理系统源码

MySQL view

Develop the first Flink app

银河麒麟v10-arm版离线安装Portainer
![[troubleshooting] common problems and solutions when installing MySQL in Windows system](/img/45/339bda7ecf8879999d8ff128c1d3ab.png)
[troubleshooting] common problems and solutions when installing MySQL in Windows system

目标检测模型大小计算,模型复杂度(参数换算公式)

第十一章 STL 之 queue
![[Luogu] p2357 tomb keeper](/img/89/34f543d9838293508252a3701f2db5.jpg)
[Luogu] p2357 tomb keeper

The inflection point of eth may be just around the corner, which is how to

第一部分—C语言基础篇_4. 程序流程结构
随机推荐
KNN分類器
Code Capriccio: question skimming record (under update)
v-mode
第八章 STL 之 vector
【C语言】自定义类型初阶知识点
Uniapp warehouse management system source code
DuiLib 实现tooltip自定义鼠标提示窗口
OpenCV模板
Simple third-party component log desensitization
Day 5 training
DEDECMS织梦文章列表标题重复显示解决方案
Data Lake (20): Flink is compatible with iceberg, which is currently insufficient, and iceberg is compared with Hudi
SAP Fiori 的附件处理(Attachment handling)
企业数字化转型,为何 SaaS 模式如此重要?
MySQL view
how to use culasLt
pytorch 调用 cublasLtMatmul 做 gemm同时加bias的代码,写的不错
【摸鱼神器】UI库秒变低代码工具——表单篇(二)子控件
OLED显示如何理解 12*6、16*8、24*12等字符大小
【论文笔记】基于深度学习的视觉检测及抓取方法