当前位置:网站首页>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;
}
};
边栏推荐
- STL教程7-set、pair对组和仿函数
- Cesium-1.72 learning (add points, lines, cubes, etc.)
- Additional: (not written yet, don't look at ~ ~ ~) corsfilter filter;
- 删除有序数组中的重复项 II[双指针--多情况统一]
- Which direction should college students choose to find jobs after graduation?
- 2022蓝桥杯国赛B组-费用报销-(线性dp|状态dp)
- 附加:(还没写,别看~~~)WebMvcConfigurer接口;
- mysql8报错:ERROR 1410 (42000): You are not allowed to create a user with GRANT解决办法
- Symantec electronic sprint technology innovation board: Tan Jian, the actual controller, is an American who plans to raise 620million yuan
- 备战数学建模34-BP神经网络预测2
猜你喜欢

More dragon lizard self-developed features! Production available Anolis OS 8.6 officially released

Etcd教程 — 第八章 Etcd之Compact、Watch和Lease API

中国传奇教授李泽湘,正在批量制造独角兽
MySQL开放远程连接权限的两种方法

【机器学习】K-means聚类分析

RT thread heap size Setting

halcon知识:区域专题【07】

The inspiration from infant cognitive learning may be the key to the next generation of unsupervised machine learning

I implement "stack" with C I

新茶饮“死去活来”,供应商却“盆满钵满”?
随机推荐
Under the pressure of technology, you can quickly get started with eth smart contract development, which will take you into the ETH world
Symantec electronic sprint technology innovation board: Tan Jian, the actual controller, is an American who plans to raise 620million yuan
halcon知识:矩阵专题【02】
Observation cloud reached in-depth cooperation with tdengine to optimize enterprise cloud experience
RTP sending PS stream zero copy scheme
猎头5万挖我去VC
On July 2, I invited you to TD Hero online conference
JS Es5 can also create constants?
Mysql8.0 method and steps for enabling remote connection permission
为了使远程工作不受影响,我写了一个内部的聊天室 | 社区征文
2022蓝桥杯国赛B组-2022-(01背包求方案数)
备战数学建模36-时间序列模型2
利用PIL进行不失真的resize
Niuke: how many different binary search trees are there
The image variables in the Halcon variable window are not displayed, and it is useless to restart the software and the computer
Distributed machine learning: model average Ma and elastic average easgd (pyspark)
[activity registration] it's your turn to explore the yuan universe! I will be waiting for you in Shenzhen on July 2!
[Verilog basics] octal and hexadecimal representation of decimal negative numbers
Talk about telecommuting | community essay solicitation
7 月 2 日邀你来TD Hero 线上发布会