1. Basic concepts
Introduction:
set is also called collection container.
- All elements will be automatically sorted when inserted
Nature:
- Set/multiset is an associative container , and the underlying structure is implemented with a binary tree .
The difference between set and multiset :
- set does not allow duplicate elements in the container
- multiset allows duplicate elements in the container
Two, construction and assignment
Function description: create set container and assign value
structure:
set<T> st;
//Default constructor:set(const set &st);
//Copy constructor
Assignment:
set& operator=(const set &st);
//Overload the equal sign operator
Example:
#include <set>
void printSet(set<int> & s)
{
for (set<int>::iterator it = s.begin(); it != s.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
//构造和赋值
void test01()
{
set<int> s1;
s1.insert(10);
s1.insert(30);
s1.insert(20);
s1.insert(40);
printSet(s1);
//拷贝构造
set<int>s2(s1);
printSet(s2);
//赋值
set<int>s3;
s3 = s2;
printSet(s3);
}
int main() {
test01();
system("pause");
return 0;
}
to sum up:
- Use insert when the set container inserts data
- The data inserted into the set container will be automatically sorted
Three, size and exchange
Function description:
- Count the size of the set container and exchange the set container
Function prototype:
size();
//Returns the number of elements in the containerempty();
//Determine whether the container is emptyswap(st);
//Exchange two collection containers
Example:
#include <set>
void printSet(set<int> & s)
{
for (set<int>::iterator it = s.begin(); it != s.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
//大小
void test01()
{
set<int> s1;
s1.insert(10);
s1.insert(30);
s1.insert(20);
s1.insert(40);
if (s1.empty())
{
cout << "s1为空" << endl;
}
else
{
cout << "s1不为空" << endl;
cout << "s1的大小为: " << s1.size() << endl;
}
}
//交换
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 << "交换前" << endl;
printSet(s1);
printSet(s2);
cout << endl;
cout << "交换后" << endl;
s1.swap(s2);
printSet(s1);
printSet(s2);
}
int main() {
//test01();
test02();
system("pause");
return 0;
}
to sum up:
- Statistics size — size
- Determine whether it is empty — empty
- Exchange container — swap
Four, insert and delete
Function description:
- set container to insert data and delete data
Function prototype:
insert(elem);
//Insert elements in the container.clear();
//Clear all elementserase(pos);
//Delete the element pointed to by the pos iterator and return the iterator of the next element.erase(beg, end);
//Delete all elements in the interval [beg,end), and return the iterator of the next element.erase(elem);
//Delete the element whose value is elem in the container.
Example:
#include <set>
void printSet(set<int> & s)
{
for (set<int>::iterator it = s.begin(); it != s.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
//插入和删除
void test01()
{
set<int> s1;
//插入
s1.insert(10);
s1.insert(30);
s1.insert(20);
s1.insert(40);
printSet(s1);
//删除
s1.erase(s1.begin());
printSet(s1);
s1.erase(30);
printSet(s1);
//清空
//s1.erase(s1.begin(), s1.end());
s1.clear();
printSet(s1);
}
int main() {
test01();
system("pause");
return 0;
}
to sum up:
- Insert — insert
- Delete — erase
- Clear — clear
Five, search and statistics
Function description:
- Find data and statistics on the set container
Function prototype:
find(key);
//Find whether the key exists, if it exists, return an iterator of the key's elements; if it does not exist, return set.end();count(key);
//Count the number of elements of the key
Example:
#include <set>
//查找和统计
void test01()
{
set<int> s1;
//插入
s1.insert(10);
s1.insert(30);
s1.insert(20);
s1.insert(40);
//查找
set<int>::iterator pos = s1.find(30);
if (pos != s1.end())
{
cout << "找到了元素 : " << *pos << endl;
}
else
{
cout << "未找到元素" << endl;
}
//统计
int num = s1.count(30);
cout << "num = " << num << endl;
}
int main() {
test01();
system("pause");
return 0;
}
to sum up:
- Find — find (returns an iterator)
- Statistics — count (for set, the result is 0 or 1)
Six, the difference between set and multiset
the difference:
- Set cannot insert duplicate data, while multiset can
- When the set inserts data, it will return the result of the insertion, indicating whether the insertion is successful
- multiset does not detect data, so duplicate data can be inserted
Example:
#include <set>
//set和multiset区别
void test01()
{
set<int> s;
pair<set<int>::iterator, bool> ret = s.insert(10);
if (ret.second) {
cout << "第一次插入成功!" << endl;
}
else {
cout << "第一次插入失败!" << endl;
}
ret = s.insert(10);
if (ret.second) {
cout << "第二次插入成功!" << endl;
}
else {
cout << "第二次插入失败!" << 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();
system("pause");
return 0;
}
to sum up:
- If it is not allowed to insert duplicate data, you can use set
- If you need to insert duplicate data, use multiset
Seven, pair creation
Function description:
- Data appearing in pairs, the use of pairs can return two data
Two creation methods:
pair<type, type> p ( value1, value2 );
pair<type, type> p = make_pair( value1, value2 );
Example:
#include <string>
//对组创建
void test01()
{
pair<string, int> p(string("Tom"), 20);
cout << "姓名: " << p.first << " 年龄: " << p.second << endl;
pair<string, int> p2 = make_pair("Jerry", 10);
cout << "姓名: " << p2.first << " 年龄: " << p2.second << endl;
}
int main() {
test01();
system("pause");
return 0;
}
to sum up:
You can create a pair in both ways, just remember one
Eight, set container sorting
Main technical points:
- Using functors, you can change the sorting rules
Example 1 set to store built-in data types
#include <set>
class MyCompare
{
public:
bool operator()(int v1, int v2) {
return v1 > v2;
}
};
void test01()
{
set<int> s1;
s1.insert(10);
s1.insert(40);
s1.insert(20);
s1.insert(30);
s1.insert(50);
//默认从小到大
for (set<int>::iterator it = s1.begin(); it != s1.end(); it++) {
cout << *it << " ";
}
cout << endl;
//指定排序规则
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();
system("pause");
return 0;
}
Note: The functor needs to be modified with const here, otherwise an error will be reported!
bool operator()(int v1,int v2) const
{
return v1 > v2;
}
Summary: Use functors to specify the sorting rules of set containers
Example 2: set stores custom data types
#include <set>
#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)
{
//按照年龄进行排序 降序
return p1.m_Age > p2.m_Age;
}
};
void test01()
{
set<Person, comparePerson> s;
Person p1("刘备", 23);
Person p2("关羽", 27);
Person p3("张飞", 25);
Person p4("赵云", 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 << "姓名: " << it->m_Name << " 年龄: " << it->m_Age << endl;
}
}
int main() {
test01();
system("pause");
return 0;
}
to sum up:
For custom data types, set must specify the sorting rules to insert data