14.8、set/multiset容器
所有元素在插入时自动排序。
set/muliset容器属于关联式容器,底层结构是用二叉树实现。
set与multiset区别:
- set不允许容器中有重复的元素。
- multiset允许容器中有重复的元素。
14.8.1、set构造和赋值
#include<iostream>
#include<set>
using namespace std;
void printset(set<int> s)
{
for (set<int>::iterator it = s.begin(); it != s.end(); it++)
{
cout << *it << ",";
}
cout << endl;
}
void test01()
{
set<int> s;
s.insert(10);//插入,只有insert
s.insert(40);
s.insert(30);
s.insert(50);
s.insert(20);
s.insert(20);//set不能重复,故插入失败
printset(s);//10,20,30,40,50,插入时被自动排序
set<int> s3(s);
printset(s3);
set<int> s2;
s2 = s;//没有assign赋值
printset(s2);
}
int main()
{
test01();
return 0;
}
14.8.2、set大小和交换
#include<iostream>
#include<set>
using namespace std;
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(40);
s1.insert(30);
s1.insert(20);
printset(s1);
if (s1.empty())
{
cout << "s1 is NULL" << endl;
}
else
{
cout << "s1 is not NULL" << endl;
cout << "s1的size是:" << s1.size() << endl;
}
set<int> s2;
s2.insert(100);
s2.insert(200);
s2.insert(300);
s2.insert(400);
printset(s2);
cout << "交换后:" << endl;
s1.swap(s2);
printset(s1);
printset(s2);
}
int main()
{
test01();
return 0;
}
14.8.3、set容器插入和删除
#include<iostream>
#include<set>
using namespace std;
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(40);
s1.insert(20);
printset(s1);
//删除
s1.erase(s1.begin());//删除指定迭代器元素
printset(s1);
s1.erase(30);//删除值为30的元素
printset(s1);
//清空
s1.clear();
//s1.erase(s1.begin(), s1.end());//区间删除
printset(s1);
}
int main()
{
test01();
return 0;
}
14.8.4、set容器查找和统计
find(key),返回元素迭代器或者end
count(key),统计key的个数——multiset结果会大于1
#include<iostream>
#include<set>
using namespace std;
void test01()
{
set<int> s1;
s1.insert(10);
s1.insert(30);
s1.insert(40);
s1.insert(20);
set<int>::iterator it;
it = s1.find(30);
if (it != s1.end())
{
cout << *it << endl;
}
else
{
cout << "未找到元素" << endl;
}
cout<<s1.count(30)<<endl;
}
int main()
{
test01();
return 0;
}
14.8.5、set和multiset区别
set不允许插入重复数据,multiset可以插入重复数据
#include<iostream>
#include<set>
using namespace std;
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<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;
}
14.8.6、pari对组的创建和使用
利用对组可以返回两个数据元素
#include<iostream>
using namespace std;
void test01()
{
pair<string, int> p("tom", 20);
cout << p.first << p.second << endl;
pair<string, int> p2 = make_pair("jerry", 30);
cout << p2.first << p2.second << endl;
}
int main()
{
test01();
return 0;
}
14.8.7、set容器排序
内置类型指定排序规则:
set默认升序排序,可以使用仿函数改变排序规则。
#include<iostream>
#include<set>
using namespace std;
class mycompare
{
public:
bool operator()(int v1,int v2)const
{
return v1 > v2;
}
};
void test01()
{
set<int> s1;
s1.insert(10);
s1.insert(30);
s1.insert(40);
s1.insert(20);
for (set<int>::iterator it = s1.begin(); it != s1.end(); it++)
{
cout << *it << ",";
}
cout << endl;
//指定排序规则为从大到小
set<int,mycompare> s2;
s2.insert(10);
s2.insert(30);
s2.insert(40);
s2.insert(20);
for (set<int, mycompare>::iterator it = s2.begin(); it != s2.end(); it++)
{
cout << *it << ",";
}
cout << endl;
}
int main()
{
test01();
return 0;
}
自定义数据类型指定排序规则:
#include<iostream>
#include<set>
#include<string>
using namespace std;
class person
{
public:
person(string name, int age)
{
this->m_age = age;
this->m_name = name;
}
int m_age;
string m_name;
};
class pcompare
{
public:
bool operator()(const person& p1, const person& p2)const
{
return p1.m_age > p2.m_age;//按年龄降序
}
};
void test01()
{
set<person,pcompare> s;//自定义数据类型需要指定排序规则
person p1("关羽", 26);
person p2("刘备", 28);
person p3("张飞", 25);
person p4("赵云", 21);
s.insert(p1);
s.insert(p2);
s.insert(p3);
s.insert(p4);
//遍历
for (set<person,pcompare>::iterator it = s.begin(); it != s.end(); it++)
{
cout << it->m_name << ",age: " << it->m_age << endl;
}
}
int main()
{
test01();
return 0;
}
14.9、map和multimap容器
map中所有元素都是pair,pair中第一个元素为key(键值),第二个元素为value(实值)
所有元素为根据key自动排序
本质:map属于关联式容器,底层用二叉树实现。
优点:根据key快速找到value
map和multimap区别:是否允许有key重复的元素出现。
14.9.1、map构造和赋值
#include<iostream>
#include<map>
using namespace std;
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));
m.insert(pair<int, int>(4, 40));
m.insert(pair<int, int>(5, 50));
printmap(m);
map<int, int> m2(m);
printmap(m2);
map<int, int> m3;
m3 = m2;
printmap(m3);
}
int main()
{
test01();
return 0;
}
14.9.2、map大小和交换
#include<iostream>
#include<map>
using namespace std;
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;
map<int, int> m2;
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 is NULL" << endl;
}
else
{
cout << "m size is: " << m.size() << endl;
}
}
void test02()
{
map<int, int> m;
map<int, int> m2;
m.insert(pair<int, int>(1, 10));
m.insert(pair<int, int>(2, 20));
m.insert(pair<int, int>(3, 30));
m2.insert(pair<int, int>(4, 100));
m2.insert(pair<int, int>(5, 200));
m2.insert(pair<int, int>(6, 300));
printmap(m);
printmap(m2);
m.swap(m2);
cout << "交换后" << endl;
printmap(m);
printmap(m2);
}
int main()
{
//test01();
test02();
return 0;
}
14.9.3、map插入和删除
#include<iostream>
#include<map>
using namespace std;
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;//map重载中括号——不建议用于插入,可以用来由key访问value
printmap(m);
//删除
m.erase(m.begin());
printmap(m);
m.erase(3);//按照key删除
printmap(m);
//m.erase(m.begin(),m.end());//按区间删——清空
m.clear();
printmap(m);
}
int main()
{
test01();
return 0;
}
14.9.4、map查找和统计
#include<iostream>
#include<map>
using namespace std;
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(make_pair(1, 10));
m.insert(make_pair(2, 20));
m.insert(make_pair(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;//map不允许插入重复的key
}
int main()
{
test01();
}
map的count只能是0或者1,而multimap的count值可以大于1
14.9.5、map容器排序
#include<iostream>
#include<map>
using namespace std;
class MyCompare
{
public:
bool operator()(int v1, int v2) const
{
//降序
return v1 > v2;
}
};
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;
}
//map默认按key升序排序
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));
m.insert(make_pair(6, 60));
//printmap(m);
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;
}
对于自定义数据类型,需要自己添加排序规则。
14.10、员工分组案例
- 招聘10个员工ABCDEFGHIJ,为员工指派工作
- 员工信息有姓名,工资组成,部门分为:策划、美术、研发
- 随机给10名员工分配部门和工资
- 通过multimap进行信息插入,key(部门编号),value(员工)
- 分部门显示员工信息
#include<iostream>
#include<string>
#include<map>
#include<vector>
#include<ctime>
using namespace std;
#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 depId = rand() % 3;//随机部门编号
m.insert(make_pair(depId, *it));
}
}
void showWorker(multimap<int, Worker>& m)
{
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);
//测试
/*for (vector<Worker>::iterator it = vWorker.begin(); it != vWorker.end(); it++)
{
cout << it->m_Name << it->m_Salary << endl;
}*/
//员工分组
multimap<int, Worker> mWorker;
setGroup(vWorker, mWorker);
//分组显示员工
showWorker(mWorker);
}
15、STL函数对象
函数对象概念:
- 重载函数调用操作符的类,其对象称为函数对象
- 函数对象使用重载的()时,行为类似于函数调用,也叫仿函数
本质:函数对象(仿函数)是一个类,不是一个函数
15.1、函数对象使用
特点:1.使用时可以像普通函数一样调用,可以有参数和返回值。2.函数对象超出普通函数的概念,可以有自己的状态。3.函数对象也可以作为参数传递。
#include<iostream>
#include<string>
using namespace std;
class MyAdd
{
public:
int operator()(int v1, int v2)
{
return v1 + v2;
}
};
void test01()
{
MyAdd myAdd;
int a = 10, b = 20;
int c = myAdd(a, b);
cout << c << endl;
}
class MyPrint
{
public:
MyPrint()
{
this->count = 0;
}
void operator()(string test)
{
cout << test << endl;
count++;
}
int count;
};
void test02()
{
MyPrint myprint;
myprint("hello world");
myprint("hello world");
myprint("hello world");
myprint("hello world");
myprint("hello world");
cout << "MyPrint调用次数为:" << myprint.count << endl;
}
void doPrint(MyPrint& myprint, string test)
{
myprint(test);
}
void test03()
{
MyPrint myprint;
doPrint(myprint, "hello C++");
}
int main()
{
//test01();
//test02();
test03();
return 0;
}
15.2、谓词
返回bool类型的仿函数成为谓词。接受一个参数为一元谓词,接受两个参数为二元谓词。
15.2.1、一元谓词
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
class GreaterFive
{
public:
bool operator()(int val)
{
return val > 5;
}
};
void test01()
{
vector<int> v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
//查找容器中有没有大于5的
vector<int>::iterator it = find_if(v.begin(), v.end(),GreaterFive());//匿名函数对象,返回值是迭代器
if (it == v.end())
{
cout << "NOT FIND" << endl;
}
else
{
cout << *it << endl;
}
}
int main()
{
test01();
}
15.2.2、二元谓词
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
//二元谓词
class MyCompare
{
public:
bool operator()(int val1, int val2)
{
return val1 > val2;
}
};
void test01()
{
vector<int> v;
v.push_back(1);
v.push_back(4);
v.push_back(2);
v.push_back(3);
v.push_back(5);
sort(v.begin(), v.end());//升序
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << ", ";
}
cout << endl;
sort(v.begin(), v.end(),MyCompare());//降序
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << ", ";
}
cout << endl;
}
int main()
{
test01();
return 0;
}
15.3、内建函数对象
STL内建了一些函数对象,分为算数仿函数,关系仿函数和逻辑仿函数,在使用时和一般函数完全相同,使用内建函数对象需要引入头文件#include<functional>
15.3.1、算术仿函数
功能:实现四则运算
negate是一元仿函数,其余为二元仿函数。
#include<iostream>
#include<functional>
using namespace std;
void test01()
{
negate<int> n;
cout << n(50) << endl;
}
void test02()
{
plus<int> p;
cout << p(10, 20) << endl;
}
int main()
{
//test01();
test02();
return 0;
}
15.3.2、关系仿函数
#include<iostream>
#include<functional>
#include<vector>
#include<algorithm>
using namespace std;
void test01()
{
vector<int> v;
v.push_back(1);
v.push_back(3);
v.push_back(4);
v.push_back(2);
v.push_back(5);
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << ", ";
}
cout << endl;
//降序
sort(v.begin(), v.end(), greater<int>());
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << ", ";
}
}
int main()
{
test01();
return 0;
}
15.3.3、逻辑仿函数
实现逻辑运算
#include<iostream>
#include<functional>
#include<vector>
#include<algorithm>
using namespace std;
void test01()
{
vector<bool> v;
v.push_back(true);
v.push_back(false);
v.push_back(true);
v.push_back(false);
v.push_back(true);
v.push_back(false);
for (vector<bool>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << ", ";
}
cout << endl;
vector<bool> v2;
v2.resize(v.size());
transform(v.begin(), v.end(), v2.begin(), logical_not<bool>());
for (vector<bool>::iterator it = v2.begin(); it != v2.end(); it++)
{
cout << *it << ", ";
}
}
int main()
{
test01();
return 0;
}