C++中set/multiset容器详解(附测试用例与结果图)
 
 
仁和
楼主
发布于 2023-4-19 14:57:38
阅读 1309
查看全部
 
 
 
 
目录 
 
1 set/ multiset 容器 
1.1 set根本概念1.2 set构造和赋值1.3 set大小和交换1.4 set插入和删除1.5 set查找和统计1.6 set和multiset区别1.7 set容器排序 
 总结 
  
 
1 set/ multiset 容器 
 
 
1.1 set根本概念 
 
简介: 
实质: 
set/multiset属于关联式容器,底层构造是用二叉树实现。 
 set和multiset区别: 
set不允许容器中有反复的元素multiset允许容器中有反复的元素 
  
1.2 set构造和赋值 
 
功能描绘:创建set容器以及赋值 
构造: 
set<T> st; //默认构造函数:set(const set &st); //拷贝构造函数 
 赋值: 
set& operator=(const set &st); //重载等号操作符 
 示例: 
#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; 
} 
 
总结: 
set容器插入数据时用insertset容器插入数据的数据会自动排序 
  
1.3 set大小和交换 
 
功能描绘: 
统计set容器大小以及交换set容器 
函数原型: 
size(); //返回容器中元素的数目empty(); //判断容器是否为空swap(st); //交换两个集合容器 
 示例: 
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; 
                cout << 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; 
} 
 
总结: 
统计大小 — size判断是否为空 — empty交换容器 — swap 
  
1.4 set插入和删除 
 
功能描绘: 
set容器停止插入数据和删除数据 
函数原型: 
insert(elem); //在容器中插入元素。clear(); //肃清所有元素erase(pos); //删除pos迭代器所指的元素,返回下一个元素的迭代器。erase(beg, end); //删除区间[beg,end)的所有元素 ,返回下一个元素的迭代器。erase(elem); //删除容器中值为elem的元素。 
 示例: 
#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; 
} 
 
总结: 
插入 — insert删除 — erase清空 — clear 
  
1.5 set查找和统计 
 
功能描绘: 
对set容器停止查找数据以及统计数据 
函数原型: 
find(key); //查找key是否存在,若存在,返回该键的元素的迭代器;若不存在,返回set.end();count(key); //统计key的元素个数 
 示例: 
#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; 
} 
 
总结: 
查找 — find (返回的是迭代器)统计 — count (对于set,结果为0或者1) 
  
1.6 set和multiset区别 
 
学习目的: 
掌握set和multiset的区别 
区别: 
set不可以插入反复数据,而multiset可以set插入数据的同时会返回插入结果,表示插入是否胜利multiset不会检测数据,因而可以插入反复数据 
 第7行的pair<set<int>::iterator, bool>是怎么来的呢?首先将鼠标停留在第七行的insert上,点击鼠标右键,选择转到定义,会发现insert函数的返回类型是_Pairib,如下图 
 
再将鼠标停留在_Pairib上,点击鼠标右键,选择转到定义,就会发现_Pairib是pair,里面有两个数据,第一个数据是迭代器 
 
示例: 
#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; 
} 
 
总结: 
假设不允许插入反复数据可以利用set假设需要插入反复数据利用multiset 
  
1.7 set容器排序 
 
学习目的: 
set容器默认排序规则为从小到大,掌握如何改变排序规则 
 主要技术点: 
利用仿函数(重载了运算符或小括号),可以改变排序规则 
 示例一: set寄存内置数据类型 
假设你想要set中的元素按自己想要的顺序停止排序,那么就要在没有插入元素前告诉set排序规则 
#include <set> 
 
class MyCompare //不一定非要是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; 
 
        //指定排序规则,<>肯定是放数据类型,不能放函数名,而仿函数Mycompare实质上就是一个类型 
        //插入数之后就没办法排序了,插入之前就要指定排序规则 
        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; 
} 
 
示例二: set寄存自定义数据类型 
#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; 
} 
 
 
总结 
 
到此这篇关于C++中set/multiset容器详解的文章就介绍到这了,更多相关C++ set/multiset容器内容请搜索网站以前的文章或继续阅读下面的相关文章希望大家以后多多支持网站! |   
 
 
 
 | 
| 
 | 
| 
 | 
| 
 |