当前位置:网站首页>STL -- set container
STL -- set container
2022-07-19 02:19:00 【hjl011006】
Catalog
One 、set Basic concept of container
Two 、set Construction and assignment
5、 ... and 、set Find and count
6、 ... and 、set and multiset The difference between
7、 ... and 、set Container sorting
8、 ... and 、set Container custom type data sorting
One 、set Basic concept of container
1. function : All elements will be automatically ordered when inserting
2. The essence :set/multiset It belongs to an associative container , The underlying structure is implemented by binary tree
3. set and multiset The difference between
set Duplicate elements are not allowed in the container
multiset Allow duplicate elements in the container
Two 、set Construction and assignment
structure :
- set<T> st; // Default constructor
- set(const set & st); // copy constructor
assignment :
- set & opeartor=(const set & st); // Overload the equal sign operator
#include<iostream>
using namespace std;
#include<set>
// Traverse
void print_set(set<int> &S)
{
for(set<int>::iterator it=S.begin();it!=S.end();it++)
{
cout<<*it<<" ";
}
cout<<endl;
}
// set Container construction and assignment
int main()
{
set<int> s1;
// insert data Only insert Methods
for(int i=0;i<10;i++)
{
s1.insert(i);
}
for(int i=0;i<10;i++)
{
s1.insert(i);
}
// Insert multiple data and store only one
// set The container will automatically sort when all elements are inserted
// set Containers are not allowed to insert duplicate values
print_set(s1);
// Copy structure
set<int> s2(s1);
print_set(s2);
// assignment
set<int> s3;
s3 = s1;
print_set(s3);
return 0;
}
3、 ... and 、set Size and swap
set Containers are not allowed to be resized , Because it will be filled with the same container , and set Containers are not allowed to have the same data
- size(); // Returns the number of elements in the container
- empty(); // Judge whether the container is empty
- swap(); // Swap two collection containers
#include<iostream>
using namespace std;
#include<set>
// Traverse
void print_set(set<int> &S)
{
for(set<int>::iterator it=S.begin();it!=S.end();it++)
{
cout<<*it<<" ";
}
cout<<endl;
}
// set Container size and swap
int main()
{
set<int> s1;
// insert data Only insert Methods
for(int i=0;i<10;i++)
{
s1.insert(i);
}
print_set(s1);
if(s1.empty())
cout<<"s1 It's empty "<<endl;
else
cout<<"s1 Not empty "<<endl;
// size
cout<<"s1 The size is :"<<s1.size()<<endl;
set<int> s2;
s2.insert(100);
s2.insert(200);
s2.insert(300);
s2.insert(400);
cout<<" Exchange before :"<<endl;
print_set(s1);
print_set(s2);
// In exchange for
s1.swap(s2);
cout<<" After exchanging :"<<endl;
print_set(s1);
print_set(s2);
return 0;
}
Four 、set Insert and delete
- insert(elem); // Insert elements into the container
- erase(pos); // Delete pos The element pointed to by the iterator , Returns the iterator of the next element
- erase(beg, end); // Delete (beg,end) All the elements in the interval , Returns the iterator of the next element
- erase(elem); // Delete the value in the container as elem The elements of
- clear(); // Empty all data in the container
#include<iostream>
using namespace std;
#include<set>
// Traverse
void print_set(set<int> &S)
{
for(set<int>::iterator it=S.begin();it!=S.end();it++)
{
cout<<*it<<" ";
}
cout<<endl;
}
// set Container insertion and deletion
int main()
{
set<int> s1;
// insert data Only insert Methods
for(int i=0;i<10;i++)
{
s1.insert(i);
}
print_set(s1);
// Delete
cout<<" Delete the data of the specified iterator \t";
s1.erase(s1.begin());
print_set(s1);
// Remove overloaded version of
// You can directly specify the content to be deleted
cout<<" Delete the specified data \t";
s1.erase(5);
print_set(s1);
// Empty operation
cout<<" Delete all elements in the interval \t";
s1.erase(s1.begin(),s1.end());
print_set(s1);
// clear Empty
s1.clear();
print_set(s1);
return 0;
}
5、 ... and 、set Find and count
- find(key); // lookup key Whether there is , If there is an iterator that returns the element , If it does not exist , return set.end();
- count(key); // Statistics key Number of elements of
#include<iostream>
using namespace std;
#include<set>
// Traverse
void print_set(set<int> &S)
{
for(set<int>::iterator it=S.begin();it!=S.end();it++)
{
cout<<*it<<" ";
}
cout<<endl;
}
// set Container search and Statistics
int main()
{
set<int> s1;
// insert data Only insert Methods
for(int i=0;i<10;i++)
{
s1.insert(i);
}
print_set(s1);
set<int>::iterator pos = s1.find(100); // What is returned is the position of the iterator , No return found s.end();
if(pos!=s1.end())
cout<<" Find the position of the number you want :"<<*pos<<endl;
else
cout<<" The number you want is not found "<<endl;
// Statistics
int num = s1.count(2);
cout<<" The number is :"<<num<<endl; // Only return 0 or 1,set Data will not be inserted repeatedly
return 0;
}
6、 ... and 、set and multiset The difference between
- set Duplicate data cannot be inserted , and multiset Sure
- set When inserting data, the result of inserting will be returned , Indicates whether the insertion was successful
- multiset It doesn't detect data , So you can insert duplicate data
#include<iostream>
using namespace std;
#include<set>
// Traverse
void print_set(set<int> &S)
{
for(set<int>::iterator it=S.begin();it!=S.end();it++)
{
cout<<*it<<" ";
}
cout<<endl;
}
void print_multiset(multiset<int> &S)
{
for(multiset<int>::iterator it=S.begin();it!=S.end();it++)
{
cout<<*it<<" ";
}
cout<<endl;
}
// set set and multiset The difference between
int main()
{
set<int> s1;
// insert data Only insert Methods
for(int i=0;i<10;i++)
{
s1.insert(i);
}
print_set(s1);
// Use pair group , Determine whether the insertion is successful
pair<set<int>::iterator,bool> ret = s1.insert(0); // Can no longer insert 0
if(ret.second)
cout<<" Insert the success "<<endl;
else
cout<<" Insert the failure "<<endl;
multiset<int> s2;
s2.insert(20);
s2.insert(20);
s2.insert(20);
s2.insert(300);
s2.insert(400);
print_multiset(s2);
return 0;
}
7、 ... and 、set Container sorting
Using affine function , You can change the sort rules
#include<iostream>
using namespace std;
#include<set>
// A functor written in a class
class my_compare
{
public:
bool operator()(int v1,int v2) // Heavy duty ()
{
return v1>v2;
}
};
// The default traversal method
void print_set(set<int> &S)
{
for(set<int>::iterator it=S.begin();it!=S.end();it++)
{
cout<<*it<<" ";
}
cout<<endl;
}
// Traverse
void print_set(set<int,my_compare> &S)
{
for(set<int,my_compare>::iterator it=S.begin();it!=S.end();it++)
{
cout<<*it<<" ";
}
cout<<endl;
}
// set Container sorting
int main()
{
set<int> s;
s.insert(10);
s.insert(40);
s.insert(67);
s.insert(23);
s.insert(89);
print_set(s);
// Set sorting rules before inserting data
set<int,my_compare> s1;
s1.insert(10);
s1.insert(40);
s1.insert(67);
s1.insert(23);
s1.insert(89);
print_set(s1);
// The default sorting rule is ascending from small to large
// Change the sorting rule to descending order from large to small
return 0;
}
8、 ... and 、set Container custom type data sorting
#include<iostream>
using namespace std;
#include<set>
// Custom data
class person
{
public:
person(string name,int age)
{
this->name=name;
this->age=age;
}
string name;
int age;
};
// functor
class my_compare
{
public:
bool operator()(const person &p1,const person &p2)
{
// According to age Descending order
return p1.age>p2.age;
}
};
// set Container row custom data sequence
int main()
{
// establish person object
person p1("TOM",20);
person p2("JERRY",30);
person p3("mike",23);
person p4("good",45);
set<person,my_compare> s1;
// For custom data types , To specify a collation
s1.insert(p1);
s1.insert(p2);
s1.insert(p3);
s1.insert(p4);
for(set<person>::iterator it=s1.begin();it!=s1.end();it++)
{
cout<<" full name :"<<it->name<<"\t Age :"<<it->age<<endl;
}
return 0;
}
边栏推荐
- ENVI_ Idl: batch re projection of modisswath products (calling the secondary development interface) + parsing
- Vmware Tools最新安装教程(RHEL8)
- 逻辑漏洞----身份验证漏洞
- 【工具篇】Unity快速上手制作2D和2.5D游戏的神器TileMap
- Visual Studio 2019-QT调试
- Difference between close and shutdown
- STL--vector容器
- 3D NFT的破茧重生:Caduceus去中心化边缘渲染技术
- bugku题解
- Hue oozie editor scheduling shell
猜你喜欢

ENVI_IDL:批量处理Modis Swath数据的重投影并输出为Geotiff格式+详细解析
![[unity Editor Extension] the pre-processing and post-processing pictures of unity assets are automatically transferred to sprite2d](/img/84/34879d5c0b6e19d599c99eec944c26.png)
[unity Editor Extension] the pre-processing and post-processing pictures of unity assets are automatically transferred to sprite2d

成信大ENVI_IDL第一周实验测试:数组的简单运算+详细解析

软件测试技术期中测试小结|软件测试基础&执行测试&测试设计和开发

成信大ENVI_IDL第二周实验内容:提取所有MODIS气溶胶产品中AOD+详细解析

gdb+vscode进行调试3——vscode以及gdb远程调试

Envi IDL: lire la teneur en colonne de NO2 de tous les produits OMI et calculer la moyenne mensuelle, la moyenne trimestrielle, la moyenne annuelle + résolution

ENVI_IDL:读取所有OMI产品的NO2柱含量并计算月均值、季均值、年均值+解析

简述特征工程及其sklearn的实现

ENVI_ Idl: read the text file and output it in GeoTIFF format + simple mean interpolation
随机推荐
Buaaos-lab0 experimental report
Vmware Tools最新安装教程(RHEL8)
[unity panel attribute literacy] set texture import settings after importing textures
Build spark on yarn environment
【Unity开发小技巧】Unity混音器Mixer控制全局音量
【工具篇】Unity2D人物控制器,控制2D玩家移动跳跃,四方向和水平方向
UE4 笔记
ENVI_IDL:批量处理Modis Swath数据的重投影并输出为Geotiff格式+详细解析
Gdb+vscode for debugging 1 -- compile and debug using cmakelist files + attach process debugging
ENVI_ Idl: read the text file and output it in GeoTIFF format + simple mean interpolation
The code of dsaa related articles in the blog
ENVI_ Idl: batch process the re projection of MODIS swath data and output it to GeoTIFF format + detailed analysis
各种开发工具
成信大ENVI_IDL第二周课堂内容:打开HDF4文件并读取文件以及简单的数据处理和保存+详细解析
成信大ENVI_IDL第二周课后作业:提取n个点的气溶胶厚度+详细解析
ENVI_ Idl: batch re projection of modisswath products (calling the secondary development interface) + parsing
Unity导入fbx模型后,运行时物体的旋转和位置自动改变的问题解决方法
vscode+ros2环境配置
ENVI_ Idl: read the NO2 column content of all OMI products and calculate the monthly average, quarterly average, annual average + analysis
逻辑漏洞---登录验证码安全