set / map 容器
set/ multiset 容器
set基本概念
簡介:
- 所有元素都會在插入時自動被排序
本質:
- set/multiset屬於關聯式容器,底層結構是用二叉樹實現。
set和multiset區別:
- set不允許容器中有重複的元素
- multiset允許容器中有重複的元素
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();
return 0;
}
運行/觀察結果: 運行後會按輸出語句打印對應內容,變量值可結合初始化、賦值和函數調用順序推導。
總結:
- set容器插入數據時用insert
- set容器插入數據的數據會自動排序
set大小和交換
功能描述:
- 統計set容器大小以及交換set容器
函數原型:
size();//返回容器中元素的數目empty();//判斷容器是否爲空swap(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);
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();
return 0;
}
運行/觀察結果: 運行後會按輸出語句打印對應內容,變量值可結合初始化、賦值和函數調用順序推導。
總結:
- 統計大小 --- size
- 判斷是否爲空 --- empty
- 交換容器 --- swap
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();
return 0;
}
運行/觀察結果: 運行後會按輸出語句打印對應內容,變量值可結合初始化、賦值和函數調用順序推導。
總結:
- 插入 --- insert
- 刪除 --- erase
- 清空 --- clear
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();
return 0;
}
運行/觀察結果: 運行後會按輸出語句打印對應內容,變量值可結合初始化、賦值和函數調用順序推導。
總結:
- 查找 --- find (返回的是迭代器)
- 統計 --- count (對於set,結果爲0或者1)
set和multiset區別
學習目標:
- 掌握set和multiset的區別
區別:
- set不可以插入重複數據,而multiset可以
- set插入數據的同時會返回插入結果,表示插入是否成功
- multiset不會檢測數據,因此可以插入重複數據
示例:
#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();
return 0;
}
運行/觀察結果: 運行後會按輸出語句打印對應內容,變量值可結合初始化、賦值和函數調用順序推導。
總結:
- 如果不允許插入重複數據可以利用set
- 如果需要插入重複數據利用multiset
pair對組創建
功能描述:
- 成對出現的數據,利用對組可以返回兩個數據
兩種創建方式:
pair<type, type> p ( value1, value2 );pair<type, type> p = make_pair( value1, value2 );
示例:
#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();
return 0;
}
運行/觀察結果: 運行後會按輸出語句打印對應內容,變量值可結合初始化、賦值和函數調用順序推導。
總結:
兩種方式都可以創建對組,記住一種即可
set容器排序
學習目標:
- set容器默認排序規則爲從小到大,掌握如何改變排序規則
主要技術點:
- 利用仿函數,可以改變排序規則
示例一 set存放內置數據類型
#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();
return 0;
}
運行/觀察結果: 運行後會按輸出語句打印對應內容,變量值可結合初始化、賦值和函數調用順序推導。
總結:利用仿函數可以指定set容器的排序規則
示例二 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();
return 0;
}
運行/觀察結果: 運行後會打印示例中的變量值或地址;地址值與運行環境有關,以同類對象的相對位置和指針變化爲觀察重點。
總結:
對於自定義數據類型,set必須指定排序規則纔可以插入數據
map/ multimap容器
map基本概念
簡介:
- map中所有元素都是pair
- pair中第一個元素爲key(鍵值),起到索引作用,第二個元素爲value(實值)
- 所有元素都會根據元素的鍵值自動排序
本質:
- map/multimap屬於關聯式容器,底層結構是用二叉樹實現。
優點:
- 可以根據key值快速找到value值
map和multimap區別:
- map不允許容器中有重複key值元素
- multimap允許容器中有重複key值元素
map構造和賦值
功能描述:
- 對map容器進行構造和賦值操作
函數原型:
構造:
map<T1, T2> mp;//map默認構造函數:map(const map &mp);//拷貝構造函數
賦值:
map& operator=(const map &mp);//重載等號操作符
示例:
#include <map>
void printMap(map<int,int>&m)
{
for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
{
cout << "key = " << it->first << " value = " << it->second << endl;
}
cout << endl;
}
void test01()
{
map<int,int>m; //默认构造
m.insert(pair<int, int>(1, 10));
m.insert(pair<int, int>(2, 20));
m.insert(pair<int, int>(3, 30));
printMap(m);
map<int, int>m2(m); //拷贝构造
printMap(m2);
map<int, int>m3;
m3 = m2; //赋值
printMap(m3);
}
int main() {
test01();
return 0;
}
運行/觀察結果: 運行後會打印示例中的變量值或地址;地址值與運行環境有關,以同類對象的相對位置和指針變化爲觀察重點。
總結:map中所有元素都是成對出現,插入數據時候要使用對組
map大小和交換
功能描述:
- 統計map容器大小以及交換map容器
函數原型:
size();//返回容器中元素的數目empty();//判斷容器是否爲空swap(st);//交換兩個集合容器
示例:
#include <map>
void printMap(map<int,int>&m)
{
for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
{
cout << "key = " << it->first << " value = " << it->second << endl;
}
cout << endl;
}
void test01()
{
map<int, int>m;
m.insert(pair<int, int>(1, 10));
m.insert(pair<int, int>(2, 20));
m.insert(pair<int, int>(3, 30));
if (m.empty())
{
cout << "m为空" << endl;
}
else
{
cout << "m不为空" << endl;
cout << "m的大小为: " << m.size() << endl;
}
}
//交换
void test02()
{
map<int, int>m;
m.insert(pair<int, int>(1, 10));
m.insert(pair<int, int>(2, 20));
m.insert(pair<int, int>(3, 30));
map<int, int>m2;
m2.insert(pair<int, int>(4, 100));
m2.insert(pair<int, int>(5, 200));
m2.insert(pair<int, int>(6, 300));
cout << "交换前" << endl;
printMap(m);
printMap(m2);
cout << "交换后" << endl;
m.swap(m2);
printMap(m);
printMap(m2);
}
int main() {
test01();
test02();
return 0;
}
運行/觀察結果: 運行後會打印示例中的變量值或地址;地址值與運行環境有關,以同類對象的相對位置和指針變化爲觀察重點。
總結:
- 統計大小 --- size
- 判斷是否爲空 --- empty
- 交換容器 --- swap
map插入和刪除
功能描述:
- map容器進行插入數據和刪除數據
函數原型:
insert(elem);//在容器中插入元素。clear();//清除所有元素erase(pos);//刪除pos迭代器所指的元素,返回下一個元素的迭代器。erase(beg, end);//刪除區間[beg,end)的所有元素 ,返回下一個元素的迭代器。erase(key);//刪除容器中值爲key的元素。
示例:
#include <map>
void printMap(map<int,int>&m)
{
for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
{
cout << "key = " << it->first << " value = " << it->second << endl;
}
cout << endl;
}
void test01()
{
//插入
map<int, int> m;
//第一种插入方式
m.insert(pair<int, int>(1, 10));
//第二种插入方式
m.insert(make_pair(2, 20));
//第三种插入方式
m.insert(map<int, int>::value_type(3, 30));
//第四种插入方式
m[4] = 40;
printMap(m);
//删除
m.erase(m.begin());
printMap(m);
m.erase(3);
printMap(m);
//清空
m.erase(m.begin(),m.end());
m.clear();
printMap(m);
}
int main() {
test01();
return 0;
}
運行/觀察結果: 運行後會打印示例中的變量值或地址;地址值與運行環境有關,以同類對象的相對位置和指針變化爲觀察重點。
總結:
- map插入方式很多,記住其一即可
- 插入 --- insert
- 刪除 --- erase
- 清空 --- clear
map查找和統計
功能描述:
- 對map容器進行查找數據以及統計數據
函數原型:
find(key);//查找key是否存在,若存在,返回該鍵的元素的迭代器;若不存在,返回set.end();count(key);//統計key的元素個數
示例:
#include <map>
//查找和统计
void test01()
{
map<int, int>m;
m.insert(pair<int, int>(1, 10));
m.insert(pair<int, int>(2, 20));
m.insert(pair<int, int>(3, 30));
//查找
map<int, int>::iterator pos = m.find(3);
if (pos != m.end())
{
cout << "找到了元素 key = " << (*pos).first << " value = " << (*pos).second << endl;
}
else
{
cout << "未找到元素" << endl;
}
//统计
int num = m.count(3);
cout << "num = " << num << endl;
}
int main() {
test01();
return 0;
}
運行/觀察結果: 運行後會按輸出語句打印對應內容,變量值可結合初始化、賦值和函數調用順序推導。
總結:
- 查找 --- find (返回的是迭代器)
- 統計 --- count (對於map,結果爲0或者1)
map容器排序
學習目標:
- map容器默認排序規則爲 按照key值進行 從小到大排序,掌握如何改變排序規則
主要技術點:
- 利用仿函數,可以改變排序規則
示例:
#include <map>
class MyCompare {
public:
bool operator()(int v1, int v2) {
return v1 > v2;
}
};
void test01()
{
//默认从小到大排序
//利用仿函数实现从大到小排序
map<int, int, MyCompare> m;
m.insert(make_pair(1, 10));
m.insert(make_pair(2, 20));
m.insert(make_pair(3, 30));
m.insert(make_pair(4, 40));
m.insert(make_pair(5, 50));
for (map<int, int, MyCompare>::iterator it = m.begin(); it != m.end(); it++) {
cout << "key:" << it->first << " value:" << it->second << endl;
}
}
int main() {
test01();
return 0;
}
運行/觀察結果: 運行後會按輸出語句打印對應內容,變量值可結合初始化、賦值和函數調用順序推導。
總結:
- 利用仿函數可以指定map容器的排序規則
- 對於自定義數據類型,map必須要指定排序規則,同set容器
案例-員工分組
案例描述
- 公司今天招聘了10個員工(ABCDEFGHIJ),10名員工進入公司之後,需要指派員工在那個部門工作
- 員工信息有: 姓名 工資組成;部門分爲:策劃、美術、研發
- 隨機給10名員工分配部門和工資
- 通過multimap進行信息的插入 key(部門編號) value(員工)
- 分部門顯示員工信息
實現步驟
- 創建10名員工,放到vector中
- 遍歷vector容器,取出每個員工,進行隨機分組
- 分組後,將員工部門編號作爲key,具體員工作爲value,放入到multimap容器中
- 分部門顯示員工信息
案例代碼:
#include<iostream>
using namespace std;
#include <vector>
#include <string>
#include <map>
#include <ctime>
/*
- 公司今天招聘了10个员工(ABCDEFGHIJ),10名员工进入公司之后,需要指派员工在那个部门工作
- 员工信息有: 姓名 工资组成;部门分为:策划、美术、研发
- 随机给10名员工分配部门和工资
- 通过multimap进行信息的插入 key(部门编号) value(员工)
- 分部门显示员工信息
*/
#define CEHUA 0
#define MEISHU 1
#define YANFA 2
class Worker
{
public:
string m_Name;
int m_Salary;
};
void createWorker(vector<Worker>&v)
{
string nameSeed = "ABCDEFGHIJ";
for (int i = 0; i < 10; i++)
{
Worker worker;
worker.m_Name = "员工";
worker.m_Name += nameSeed[i];
worker.m_Salary = rand() % 10000 + 10000; // 10000 ~ 19999
//将员工放入到容器中
v.push_back(worker);
}
}
//员工分组
void setGroup(vector<Worker>&v,multimap<int,Worker>&m)
{
for (vector<Worker>::iterator it = v.begin(); it != v.end(); it++)
{
//产生随机部门编号
int deptId = rand() % 3; // 0 1 2
//将员工插入到分组中
//key部门编号,value具体员工
m.insert(make_pair(deptId, *it));
}
}
void showWorkerByGourp(multimap<int,Worker>&m)
{
// 0 A B C 1 D E 2 F G ...
cout << "策划部门:" << endl;
multimap<int,Worker>::iterator pos = m.find(CEHUA);
int count = m.count(CEHUA); // 统计具体人数
int index = 0;
for (; pos != m.end() && index < count; pos++ , index++)
{
cout << "姓名: " << pos->second.m_Name << " 工资: " << pos->second.m_Salary << endl;
}
cout << "----------------------" << endl;
cout << "美术部门: " << endl;
pos = m.find(MEISHU);
count = m.count(MEISHU); // 统计具体人数
index = 0;
for (; pos != m.end() && index < count; pos++, index++)
{
cout << "姓名: " << pos->second.m_Name << " 工资: " << pos->second.m_Salary << endl;
}
cout << "----------------------" << endl;
cout << "研发部门: " << endl;
pos = m.find(YANFA);
count = m.count(YANFA); // 统计具体人数
index = 0;
for (; pos != m.end() && index < count; pos++, index++)
{
cout << "姓名: " << pos->second.m_Name << " 工资: " << pos->second.m_Salary << endl;
}
}
int main() {
srand((unsigned int)time(NULL));
//1、创建员工
vector<Worker>vWorker;
createWorker(vWorker);
//2、员工分组
multimap<int, Worker>mWorker;
setGroup(vWorker, mWorker);
//3、分组显示员工
showWorkerByGourp(mWorker);
////测试
//for (vector<Worker>::iterator it = vWorker.begin(); it != vWorker.end(); it++)
//{
// cout << "姓名: " << it->m_Name << " 工资: " << it->m_Salary << endl;
//}
return 0;
}
運行/觀察結果: 這段是語法片段,重點看寫法;補全上下文後再運行。
總結:
- 當數據以鍵值對形式存在,可以考慮用map 或 multimap