当前位置:网站首页>STL tutorial 7-set, pair pair pair group and functor
STL tutorial 7-set, pair pair pair group and functor
2022-06-30 16:49:00 【Sleepy snail】
Catalog
One 、set Containers
- set and multiset It's a collection container , among set The contained elements are unique , The elements in the collection are automatically arranged in a certain order .set Using the data structure of red black tree variant to realize , Red black trees belong to balanced binary trees . It's better than... In insert and delete operations vector fast , stay n The efficiency of finding the target number in the number is log 2 n
- set In the container Duplicate elements are not allowed ( The Chinese name set means , There can only be one ),multiset Allow repeating elements .
- Only available Insert Method initialization
- set/multiset The feature of is that all elements are automatically sorted according to the value of the element
- set A value cannot be modified , If you want to modify, you can only delete and add
1、set Constructors
set<int> s1;// Automatically sort , Default from small to large .
s1.insert(7);
s1.insert(2);
s1.insert(3);// At this time, it will be printed 2 3 7, Has been automatically sorted
2、set Assignment operation
set<int> s2;
s2 = s1;
s2.swap(s1);//swap Swap collection containers
3、set Size operation
size();// Get the number of container elements
empty();// Determine whether the inner container is empty
4、set Insert and delete operations
insert(elem);// Insert elements into the container
clear();// Remove all elements
erase(pos);// Delete the element that the iterator refers to , Both of these are iterators
erase(beg,end);// Delete element of interval
erase(elem);// Delete the value in the container as elem The elements of , Yes delete one , Although there is only one
5、set Lookup ( important )
s1.find(4) look for 4 Iterator of position
lower_bound(2) Find the first one greater than or equal to 2 Of ( Elements ) Iterator value
s1.upper_bound(2); Find the first one greater than K Value
set<int> s1;
// Insert several elements
s1.insert(7);
s1.insert(2);
s1.insert(4);
s1.insert(5);
s1.insert(1);
s1.insert(9);
// Use iterators to find elements from scratch
//find Function to find elements , An iterator that returns the position of an element , If no return is found end() The iterator
set<int>::iterator ret = s1.find(4);
// Next, determine whether the element is found
if (ret == s1.end())
{
cout << " Can't find " << endl;
}
else
{
cout << "ret:" << *ret << endl;
}
//---------------------------------------------------------------
//lower_bound(2) Find the first one greater than or equal to k Of ( Elements ) Iterator value
ret = s1.lower_bound(2);
if (ret == s1.end())
{
cout << " Can't find " << endl;
}
else
{
cout << "ret:" << *ret << endl;
}
//---------------------------------------------------------------
// Find the first one greater than K Value
ret = s1.upper_bound(2);
if (ret == s1.end())
{
cout << " Can't find " << endl;
}
else
{
cout << "ret:" << *ret << endl;
}
//equal_range return Lower_bound and upper_bound value
pair<set<int>::iterator, set<int>::iterator> myret = s1.equal_range(2);
// It's used here pair We will add the relevant contents of the group below .
/*myret.first; myret.second;*/
if (myret.first == s1.end())
{
cout << "can't find" << endl;
}
else
{
cout << "myret:" << *myret.first << endl;// return Lower_bound
}
if (myret.second == s1.end())
{
cout << "can't find" << endl;
}
else
{
cout << "myret:" << *myret.second << endl; // return upper_bound
}
6、 modify set Sort order of
Look at the following functor
set<int, mycompare> s3;// After using the functor here , The default insertion order will be modified during insertion
s3.insert(7);
s3.insert(2);
s3.insert(4);
s3.insert(5);
s3.insert(1);
s3.insert(9);
7、set Add objects to
set Adding elements requires sorting , But if you sort directly ,set If you don't know how to arrange, you will report an error , So we also need an affine function
// Define a class
class Person
{
public:
Person(int age, int id):id(id),age(age){
}
public:
int id;
int age;
};
// Define a functor , Sort the objects according to their age
class mycompare2
{
public:
bool operator()(Person p1, Person p2)const
{
return p1.age > p2.age;
}
};
void test(){
// Use functor to customize sorting
set<Person,mycompare2> sp;
Person p1(10, 20), p2(30, 40), p3(50, 60);
sp.insert(p1);
sp.insert(p2);
sp.insert(p3);
}
It should be noted that , If you use find, At this point, you should look for the element to compare , For example, it is used above age To compare , This is also used when searching age Go find , This age It's like a key . For example, the following code , Although I didn't join p4, however p4 Inside 40 Is already joined , So we can find
Person p4(10, 40);
auto ret = sp.find(p4);//set<Person,mycompare2>::iterator
if (ret == sp.end())
{
cout << "can't find" << endl;
}
else
{
cout << "find:" << (*ret).id << " " << (*ret).age << endl;
}
Two 、pair
pair Yes, it will 2 Data is combined into a set of data , When you need such a requirement, you can use pair, Such as stl Medium map Will be key and value Put them together to keep . Another application is , When a function needs to return 2 Data , You can choose pair.
// Law 1
pair<string, int> pair1(string("number"), 20);
cout << pair1.first << " " << pair1.second << endl;
// perhaps pair<string,int> pair2=make_pair("name",30)
pair<string, int> pair2 = make_pair("name", 30);
cout << pair2.first << " " << pair2.second << endl;
//pair assignment
pair<string, int> pair3 = pair2;
cout << pair2.first << " " << pair2.second << endl;
3、 ... and 、 functor
// functor
class mycompare
{
public:
bool operator()(int v1,int v2)const
{
return v1 > v2;
}
};
边栏推荐
- 备战数学建模33-灰色预测模型2
- POJ Project Summer
- BC1.2 PD协议
- Raft介绍
- [machine learning] K-means clustering analysis
- MySQL8.0开启远程连接权限的方法步骤
- Half year inventory of new consumption in 2022: the industry is cold, but these nine tracks still attract gold
- ArcMap operation series: 80 plane to latitude and longitude 84
- What role does "low code" play in enterprise digital transformation?
- Good partner for cloud skill improvement, senior brother cloud of Amazon officially opened today
猜你喜欢
删除有序数组中的重复项 II[双指针--多情况统一]
RT thread heap size setting
Mysql8 error: error 1410 (42000): you are not allowed to create a user with grant solution
居家办公浅谈远程协助快速提效心得 | 社区征文
Under the pressure of technology, you can quickly get started with eth smart contract development, which will take you into the ETH world
Two methods for MySQL to open remote connection permission
香港回归25周年 香港故宫博物馆正式开放成文化新地标
Symantec electronic sprint technology innovation board: Tan Jian, the actual controller, is an American who plans to raise 620million yuan
HMS Core音频编辑服务3D音频技术,助力打造沉浸式听觉盛宴
备战数学建模33-灰色预测模型2
随机推荐
Asp. NETCORE uses cache and AOP to prevent repeated commit
腾讯二面:@Bean 与 @Component 用在同一个类上,会怎么样?
2022新消费半年盘点:行业遇冷,但这九个赛道依然吸金
【微信小程序】小程序的宿主环境
RT-Thread 堆區大小設置
备战数学建模33-灰色预测模型2
Home office discussion on the experience of remote assistance to quickly improve efficiency | community essay solicitation
[machine learning] K-means clustering analysis
Tencent two sides: @bean and @component are used on the same class. What happens?
Rong Lianyun launched rphone based on Tongxin UOS to create a new ecology of localization contact center
How cloudxr promotes the future development of XR
Hundreds of lines of code to implement a JSON parser
2020 Blue Bridge Cup group B - move bricks - (greedy sorting +01 backpack)
RTP 发送PS流零拷贝方案
Hologres共享集群助力淘宝订阅极致精细化运营
备战数学建模36-时间序列模型2
容联云首发基于统信UOS的Rphone,打造国产化联络中心新生态
利用PIL进行不失真的resize
Half year inventory of new consumption in 2022: the industry is cold, but these nine tracks still attract gold
Mathematical modeling for war preparation 33- grey prediction model 2