当前位置:网站首页>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第二周课后作业:提取n个点的气溶胶厚度+详细解析

项目性能优化实战:解决首页白屏问题,自定义 loading 动画优化首屏效果

ENVI_IDL:批量重投影ModisSwath产品(调用二次开发接口)+解析

新手如何配置多个 SSH Key(通俗易懂手把手教学)

Gdb+vscode for debugging 5 - GDB view relevant commands

ENVI_ Idl: average calculation + analysis of MODIS swath products in batches

bugku----正则匹配,cookies

笔记一之IDL基础内容:常用数据类型_创建数组_类型转换_print输出_基本运算_关系运算

Summary of tree and heap knowledge points

gdb+vscode进行调试3——vscode以及gdb远程调试
随机推荐
gdb+vscode进行调试1——使用CMakelist文件进行编译和调试+附加进程调试
Unity笔记1
成信大ENVI_IDL第二周实验内容:提取所有MODIS气溶胶产品中AOD+详细解析
各种开发工具
ENVI_IDL:读取所有OMI产品的NO2柱含量并计算月均值、季均值、年均值+解析
Foo bar what the hell?
工程编译那点事:Makefile和cmake(一)
gdb+vscode进行调试——release版本如何调试
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
Leetcode 1: Two Sum
成信大ENVI_IDL第三周课堂内容1:读取OMI数据(HDF5文件)以及输出+解析
ENVI_IDL:批量拼接Modis Swath的逐日数据并输出为Geotiff格式
ENVI_ Idl: average calculation + analysis of MODIS swath products in batches
散列表、布隆过滤器、分布式一致性hash
ENVI_ Idl: batch re projection of modisswath products (calling the secondary development interface) + parsing
YYDS! The latest distributed core technology notes summarized by Alibaba technical officers have been launched, which can be regarded as a blessing
Oozie integrated sh
字符串转换为整数
Double Q-Learning理论基础及其代码实现【Pendulum-v0】
【Unity编辑器扩展】Unity发布资源到商店流程Unity Asset Store Publish