本文主要是介绍c++基础学习第九天(stl),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
c++基础学习第九天(stl)
文章目录
- 2.8、 set/ multiset 容器
- 2.8.1、 set基本概念
- 2.8.2、 set构造和赋值
- 2.8.3、 set大小和交换
- 2.8.4、 set插入和删除
- 2.8.5、 set查找和统计
- 2.8.6、 set和multiset区别
- 2.8.7、 pair对组创建
- 2.8.8、 set容器排序
- 2.9、 map/ multimap容器
- 2.9.1、 map基本概念
- 2.9.2、 map构造和赋值
- 2.9.3、 map大小和交换
- 2.9.4、 map插入和删除
- 2.9.5、 map查找和统计
- 2.9.6、 map容器排序
- 2.10、 案例-员工分组
- 2.10.1、 案例描述
- 2.10.2、 实现步骤
- 3、 STL- 函数对象
- 3.1、 函数对象
- 3.1.1、 函数对象概念
- 3.1.2、 函数对象使用
- 3.2、 谓词
- 3.2.1、 谓词概念
- 3.2.2、 一元谓词
- 3.2.3、 二元谓词
- 3.3、 内建函数对象
- 3.3.1、 内建函数对象意义
- 3.3.2、 算术仿函数
- 3.3.3、 关系仿函数
- 3.3.4、 逻辑仿函数
2.8、 set/ multiset 容器
2.8.1、 set基本概念
简介:
- 所有元素都会在插入时自动被排序
本质:
- set/multiset属于关联式容器,底层结构是用二叉树实现。
set和multiset区别:
- set不允许容器中有重复的元素
- multiset允许容器中有重复的元素
2.8.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容器插入数据时用insert
- set容器插入数据的数据会自动排序
2.8.3、 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();system("pause");return 0;
}
总结:
- 统计大小 — size
- 判断是否为空 — empty
- 交换容器 — swap
2.8.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
2.8.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)
2.8.6、 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;}//multisetmultiset<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
2.8.7、 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();system("pause");return 0;
}
总结:
两种方式都可以创建对组,记住一种即可
2.8.8、 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();system("pause");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();system("pause");return 0;
}
总结:
对于自定义数据类型,set必须指定排序规则才可以插入数据
2.9、 map/ multimap容器
2.9.1、 map基本概念
简介:
- map中所有元素都是pair
- pair中第一个元素为key(键值),起到索引作用,第二个元素为value(实值)
- 所有元素都会根据元素的键值自动排序
本质:
- map/multimap属于关联式容器,底层结构是用二叉树实现。
优点:
- 可以根据key值快速找到value值
map和multimap区别:
- map不允许容器中有重复key值元素
- multimap允许容器中有重复key值元素
2.9.2、 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();system("pause");return 0;
}
总结:map中所有元素都是成对出现,插入数据时候要使用对组
2.9.3、 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();system("pause");return 0;
}
总结:
- 统计大小 — size
- 判断是否为空 — empty
- 交换容器 — swap
2.9.4、 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();system("pause");return 0;
}
总结:
- map插入方式很多,记住其一即可
- 插入 — insert
- 删除 — erase
- 清空 — clear
2.9.5、 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();system("pause");return 0;
}
总结:
- 查找 — find (返回的是迭代器)
- 统计 — count (对于map,结果为0或者1)
2.9.6、 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();system("pause");return 0;
}
总结:
- 利用仿函数可以指定map容器的排序规则
- 对于自定义数据类型,map必须要指定排序规则,同set容器
2.10、 案例-员工分组
2.10.1、 案例描述
- 公司今天招聘了10个员工(ABCDEFGHIJ),10名员工进入公司之后,需要指派员工在那个部门工作
- 员工信息有: 姓名 工资组成;部门分为:策划、美术、研发
- 随机给10名员工分配部门和工资
- 通过multimap进行信息的插入 key(部门编号) value(员工)
- 分部门显示员工信息
2.10.2、 实现步骤
- 创建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 2class 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;//}system("pause");return 0;
}
总结:
- 当数据以键值对形式存在,可以考虑用map 或 multimap
3、 STL- 函数对象
3.1、 函数对象
3.1.1、 函数对象概念
概念:
- 重载函数调用操作符的类,其对象常称为函数对象
- 函数对象使用重载的()时,行为类似函数调用,也叫仿函数
本质:
函数对象(仿函数)是一个类,不是一个函数
3.1.2、 函数对象使用
特点:
- 函数对象在使用时,可以像普通函数那样调用, 可以有参数,可以有返回值
- 函数对象超出普通函数的概念,函数对象可以有自己的状态
- 函数对象可以作为参数传递
示例:
#include <string>//1、函数对象在使用时,可以像普通函数那样调用, 可以有参数,可以有返回值
class MyAdd
{
public :int operator()(int v1,int v2){return v1 + v2;}
};void test01()
{MyAdd myAdd;cout << myAdd(10, 10) << endl;
}//2、函数对象可以有自己的状态
class MyPrint
{
public:MyPrint(){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");cout << "myPrint调用次数为: " << myPrint.count << endl;
}//3、函数对象可以作为参数传递
void doPrint(MyPrint &mp , string test)
{mp(test);
}void test03()
{MyPrint myPrint;doPrint(myPrint, "Hello C++");
}int main() {//test01();//test02();test03();system("pause");return 0;
}
总结:
- 仿函数写法非常灵活,可以作为参数进行传递。
3.2、 谓词
3.2.1、 谓词概念
概念:
- 返回bool类型的仿函数称为谓词
- 如果operator()接受一个参数,那么叫做一元谓词
- 如果operator()接受两个参数,那么叫做二元谓词
3.2.2、 一元谓词
示例:
#include <vector>
#include <algorithm>//1.一元谓词
struct GreaterFive{bool operator()(int val) {return val > 5;}
};void test01() {vector<int> v;for (int i = 0; i < 10; i++){v.push_back(i);}vector<int>::iterator it = find_if(v.begin(), v.end(), GreaterFive());if (it == v.end()) {cout << "没找到!" << endl;}else {cout << "找到:" << *it << endl;}}int main() {test01();system("pause");return 0;
}
总结:参数只有一个的谓词,称为一元谓词
3.2.3、 二元谓词
示例:
#include <vector>
#include <algorithm>
//二元谓词
class MyCompare
{
public:bool operator()(int num1, int num2){return num1 > num2;}
};void test01()
{vector<int> v;v.push_back(10);v.push_back(40);v.push_back(20);v.push_back(30);v.push_back(50);//默认从小到大sort(v.begin(), v.end());for (vector<int>::iterator it = v.begin(); it != v.end(); it++){cout << *it << " ";}cout << endl;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();system("pause");return 0;
}
总结:参数只有两个的谓词,称为二元谓词
3.3、 内建函数对象
3.3.1、 内建函数对象意义
概念:
- STL内建了一些函数对象
分类:
-
算术仿函数
-
关系仿函数
-
逻辑仿函数
用法:
- 这些仿函数所产生的对象,用法和一般函数完全相同
- 使用内建函数对象,需要引入头文件
#include<functional>
3.3.2、 算术仿函数
功能描述:
- 实现四则运算
- 其中negate是一元运算,其他都是二元运算
仿函数原型:
template<class T> T plus<T>
//加法仿函数template<class T> T minus<T>
//减法仿函数template<class T> T multiplies<T>
//乘法仿函数template<class T> T divides<T>
//除法仿函数template<class T> T modulus<T>
//取模仿函数template<class T> T negate<T>
//取反仿函数
示例:
#include <functional>
//negate
void test01()
{negate<int> n;cout << n(50) << endl;
}//plus
void test02()
{plus<int> p;cout << p(10, 20) << endl;
}int main() {test01();test02();system("pause");return 0;
}
总结:使用内建函数对象时,需要引入头文件 #include <functional>
3.3.3、 关系仿函数
功能描述:
- 实现关系对比
仿函数原型:
template<class T> bool equal_to<T>
//等于template<class T> bool not_equal_to<T>
//不等于template<class T> bool greater<T>
//大于template<class T> bool greater_equal<T>
//大于等于template<class T> bool less<T>
//小于template<class T> bool less_equal<T>
//小于等于
示例:
#include <functional>
#include <vector>
#include <algorithm>class MyCompare
{
public:bool operator()(int v1,int v2){return v1 > v2;}
};
void test01()
{vector<int> v;v.push_back(10);v.push_back(30);v.push_back(50);v.push_back(40);v.push_back(20);for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {cout << *it << " ";}cout << endl;//自己实现仿函数//sort(v.begin(), v.end(), MyCompare());//STL内建仿函数 大于仿函数sort(v.begin(), v.end(), greater<int>());for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {cout << *it << " ";}cout << endl;
}int main() {test01();system("pause");return 0;
}
总结:关系仿函数中最常用的就是greater<>大于
3.3.4、 逻辑仿函数
功能描述:
- 实现逻辑运算
函数原型:
template<class T> bool logical_and<T>
//逻辑与template<class T> bool logical_or<T>
//逻辑或template<class T> bool logical_not<T>
//逻辑非
示例:
#include <vector>
#include <functional>
#include <algorithm>
void test01()
{vector<bool> v;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;//逻辑非 将v容器搬运到v2中,并执行逻辑非运算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 << " ";}cout << endl;
}int main() {test01();system("pause");return 0;
}
总结:逻辑仿函数实际应用较少,了解即可
这篇关于c++基础学习第九天(stl)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!