在學習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 名員工進入公司之後,需要指派員工在那個部門工作