天天看点

c++基础学习笔记——05-c++STLday12

在学习c++提高-STL总结了笔记,并分享出来。有问题请及时联系博主:​​Alliswell_WP​​,转载请注明出处。

05-c++STLday12

一、上节作业——multimap案例

//公司今天招聘了5个员工,5名员工进入公司之后,需要指派员工在那个部门工作

//人员信息有: 姓名 年龄 电话 工资等组成

//通过Multimap进行信息的插入 保存 显示

//分部门显示员工信息 显示全部员工信息

代码如下:

1 #define _CRT_SECURE_NO_WARNINGS
  2 #include<iostream>
  3 using namespace std;
  4 #include<string>
  5 #include<vector>
  6 #include<map>
  7 #include<ctime>
  8 /*
  9 //公司今天招聘了5个员工,5名员工进入公司之后,需要指派员工在那个部门工作
 10 //人员信息有: 姓名 年龄 电话 工资等组成
 11 //通过Multimap进行信息的插入 保存 显示
 12 //分部门显示员工信息 显示全部员工信息
 13 
 14 */
 15 
 16 enum{ RENLI, YANFA,MEISHU};
 17 
 18 class Worker
 19 {
 20     
 21 public:
 22     string m_Name;
 23     int m_Money;
 24 };
 25 
 26 void createWorker(vector<Worker>& v)
 27 {
 28     string nameSeed = "ABCDE";
 29     for(int i = 0; i < 5; i++)
 30     {
 31         string name = "员工";
 32         name += nameSeed[i];
 33         
 34         int money = rand() % 10000 + 10000;//10000~19999
 35         
 36         Worker w;
 37         w.m_Name = name;
 38         w.m_Money = money;
 39         
 40         v.push_back(w);
 41     }
 42     
 43 }
 44 
 45 void setGroup(vector<Worker>& v, multimap<int, Worker>& m)
 46 {
 47     for(vector<Worker>::iterator it = v.begin(); it != v.end(); it++)
 48     {
 49         //随机产生部门编号
 50         int departmentId = rand() % 3;//0 1 2
 51         //将员工分到multimap容器中
 52         m.insert(make_pair(departmentId, *it));
 53     }
 54     
 55 }
 56 
 57 void showGroup(multimap<int, Worker>& m)
 58 {
 59     //人力部门显示
 60     cout << "人力部门员工如下:" << endl;
 61     
 62     //0 A B 1 C 2 D E
 63     //如果找不到,会报错吗?
 64     multimap<int, Worker>::iterator pos = m.find(RENLI);
 65     int index = 0;
 66     int num = m.count(RENLI);
 67     for(; pos != m.end() && index < num; pos++, index++)
 68     {
 69         cout << "姓名:" << pos->second.m_Name << "工资:" << pos->second.m_Money << endl;
 70     }
 71     
 72     cout << "------------------" << endl;
 73     
 74     cout << "研发部门员工如下:" << endl;
 75     
 76     //0 A B 1 C 2 D E
 77     pos = m.find(YANFA);
 78     index = 0;
 79     num = m.count(YANFA);
 80     for(; pos != m.end() && index < num; pos++, index++)
 81     {
 82         cout << "姓名:" << pos->second.m_Name << "工资:" << pos->second.m_Money << endl;
 83     }
 84     
 85     cout << "------------------" << endl;
 86     
 87     cout << "美术部门员工如下:" << endl;
 88     
 89     //0 A B 1 C 2 D E
 90     pos = m.find(MEISHU);
 91     index = 0;
 92     num = m.count(MEISHU);
 93     for(; pos != m.end() && index < num; pos++, index++)
 94     {
 95         cout << "姓名:" << pos->second.m_Name << "工资:" << pos->second.m_Money << endl;
 96     }
 97     
 98 }
 99 
100 
101 void test01()
102 {
103     //最后添加随机数种子
104     srand((unsigned int)time(NULL));
105     
106     //声明一个存放员工的容器
107     vector<Worker>v;
108     
109     //创建5名员工
110     createWorker(v);
111     
112     /*
113     //员工创建测试
114     for(vector<Worker>::iterator it = v.begin(); it != v.end(); it++)
115     {
116         cout << "员工姓名:" << it->m_Name << "工资:" << it->m_Money << endl;
117     }
118     */
119     
120     //设置分组
121     //分组的multimap容器
122     multimap<int, Worker>m;
123     setGroup(v, m);
124     
125     //分部门显示员工
126     showGroup(m);
127 }
128 
129 int main()
130 {
131     test01();
132     
133     system("pause");
134     return EXIT_SUCCESS;
135 }      

二、常用算法

1、函数对象

练习:

1 #define _CRT_SECURE_NO_WARNINGS
 2 #include<iostream>
 3 using namespace std;
 4 
 5 
 6 class MyPrint
 7 {
 8 public:
 9     void operator()(int num)
10     {
11         cout << "num:" << num << endl;
12         count++;
13     }
14     int count = 0;
15 };
16 
17 void MyPrint2(int num)
18 {
19     cout << "num:" << num << endl;
20 }
21 
22 void test01()
23 {
24     //MyPrint是一个类,而不是函数
25     MyPrint myPrint;
26     myPrint(111);
27     
28     //MyPrint2(111);//普通函数调用
29     
30     MyPrint()(1000);//匿名对象调用
31     
32 }
33 
34 //函数对象超出普通函数概念,内部可以保存状态
35 void test02()
36 {
37     MyPrint myPrint;
38     myPrint(111);
39     myPrint(111);
40     myPrint(111);
41     myPrint(111);
42     
43     cout << "myPrint使用次数:" << myPrint.count << endl;
44 }
45 
46 //函数对象作为参数
47 void doPrint(MyPrint print, int num)
48 {
49     print(num);
50 }
51 
52 void test03()
53 {
54     doPrint(MyPrint(), 20);
55 }
56 
57 int main()
58 {
59     test01();
60     
61     system("pause");
62     return EXIT_SUCCESS;
63 }      

2、谓词

1 #define _CRT_SECURE_NO_WARNINGS
 2 #include<iostream>
 3 using namespace std;
 4 #include<vector>
 5 #include<algorithm>
 6 
 7 class GreaterThan20
 8 {
 9 public:
10     bool operator()(int val)
11     {
12         return val > 20;
13     }
14     
15     
16 };
17 
18 
19 //一元谓词
20 void test01()
21 {
22     vector<int>v;
23     v.push_back(10);
24     v.push_back(20);
25     v.push_back(30);
26     v.push_back(40);
27     v.push_back(50);
28     
29     //查找第一个大于20的数字
30     //第三个参数为:函数对象,匿名对象
31     vecot<int>::iterator pos = find_if(v.begin(), v.end(),GreaterThan20());
32     if(pos != v.end())
33     {
34         cout << "找到大于20的数字为:" << *pos << endl;
35     }
36     else
37     {
38         cout << "未找到" << endl;
39     }
40     
41 }
42 
43 //二元谓词
44 class MyCompare
45 {
46 public:
47     bool operator()(int v1, int v2)
48     {
49         return v1 > v2;
50     }
51     
52 };
53 void test02()
54 {
55     vector<int>v;
56     v.push_back(10);
57     v.push_back(20);
58     v.push_back(30);
59     v.push_back(40);
60     v.push_back(50);
61     
62     sort(v.begin(), v.end(), MyCompare());
63     
64     //匿名函数:lambda表达式[](){};
65     for_each(v.begin(), v.end(), [](int val){ cout << val << " ";});
66 }
67 
68 
69 int main()
70 {
71     test01();
72     
73     system("pause");
74     return EXIT_SUCCESS;
75 }      

3、内建函数对象

4、

三、总结

1    函数对象(仿函数)

1.1    重载 () 所以函数的对象 使用()像函数调用

1.2    是类 而不是普通的函数

1.3    内部记录状态

1.4    作为类型 与模板进行配合使用

2    谓词

2.1    普通函数或者仿函数返回值 bool类型

2.2    一元 一个参数 二元 两个参数

2.3    一元 查找 大于20的数字   find_if 返回迭代器

2.4    二元 排序  

3    内建函数对象

3.1    取反

3.2    加法

3.3    大于  greater<int>()

4    适配器

4.1    函数适配器

4.2    0~9 加起始值 进行输出 用户提供起始值

4.3    bind2nd  绑定

4.4    继承  binary_function<参数类型1,参数类型2,返回值类型>

4.5    const修饰 operator()  

4.6    取反适配器

4.6.1    not1  一元 找出小于5  

4.6.2    not2 二元  排序  not2(  less<int>() ) 从大到小 相当于  greater<int>()

4.7    普通函数指针适配  

4.7.1    ptr_fun

4.8    成员函数适配

4.8.1    //如果容器存放的是对象指针,  那么用mem_fun

4.8.2    //如果容器中存放的是对象实体,那么用mem_fun_ref    

5    常用遍历算法

5.1    for_each  可有有返回值

5.2    可以绑定参数进行输出

5.3    transform  将容器中的数据进行搬运到另一个容器中

5.4    注意:目标容器需要开辟空间

6    常用查找算法

6.1    find  按值查找 Person

6.2    find_if 按条件查找 Person*

6.3    adjacent_find算法 查找相邻重复元素 返回第一个重复元素的迭代器位置

6.4    binary_search算法 二分查找法 必须容器是有序序列

6.5    count 和count_if

7    常用排序算法

7.1    merge算法 容器元素合并,并存储到另一容器中,两容器要有序,并且顺序一致

7.2    sort 排序

7.3    random_shuffle 洗牌  自己提供随机种子

7.4    reverse反转

8    常用的拷贝和替换算法

8.1    copy复制

8.2    replace  replace_if 替换

8.3    swap 交换

9    常用算数生成算法

9.1    头文件 numeric

9.2    accumulate 累加

9.3    fill 填充

10    常用集合算法

10.1    交集 set_intersection

10.2    并集 set_union

10.3    差集 set_difference

在学习c++提高-STL总结了笔记,并分享出来。有问题请及时联系博主:​​Alliswell_WP​​,转载请注明出处。

//multimap 案例

//公司今天招聘了 5 个员工,5 名员工进入公司之后,需要指派员工在那个部门工作

继续阅读