概述:
-算法主要是由<algorithm> <functional> <numeric> 组成
-<algorithm>是所有STL头文件中最大的一个,范围涉及到 比较、交换、查找、遍历、复制、修改等等
-<numeric>体积最小,只包含几个在序列上面进行简单数学运算的模板函数
-<functional>定义了一些模板类,用以声明函数对象
1.常用的遍历算法
-for_each //遍历容器
-transform //搬运容器到另一个容器中
1 #include <iostream> 2 #include <vector> 3 using namespace std; 4 #include <algorithm> 5 /* 6 非常常用,用熟练掌握 7 for_each(iterator_begin,iterator_end,func); 8 iterator_begin 开始迭代器 9 iterator_end 结束迭代器 10 func 函数或函数对象 11 */ 12 13 class Print02 { 14 public: 15 bool operator()(int val) 16 { 17 cout << val << " "; 18 } 19 //cout << endl; 20 }; 21 void Print01(int it)//注意此处不是容器 22 { 23 cout << it << " ";//此处不是迭代器 24 } 25 void test01() 26 { 27 vector<int>v; 28 29 for (int i = 0; i < 10; i++) 30 { 31 v.push_back(i); 32 } 33 34 //第一种 35 for_each(v.begin(), v.end(), Print01); 36 cout << endl; 37 //第二种 38 for_each(v.begin(), v.end(), Print02()); 39 } 40 int main() 41 { 42 test01(); 43 return 0; 44 }
1 #include <iostream> 2 #include <vector> 3 #include <algorithm> 4 using namespace std; 5 /* 6 transform(iterator_begin,iterator_end,iterator_begin2,func); 7 iterator_begin 源容器开始迭代器 8 iterator_begin2 目标容器开始迭代器 9 */ 10 class Transform { 11 public: 12 bool operator()(int val) 13 { 14 return val+100; 15 } 16 }; 17 class myPrint { 18 public: 19 bool operator()(int val) 20 { 21 cout << val << " "; 22 } 23 }; 24 void test01() 25 { 26 vector<int>v; 27 for (int i = 1; i <= 10; i++) 28 { 29 v.push_back(i); 30 } 31 32 vector<int>vTarg; 33 vTarg.resize(v.size());//提前开辟空间 34 transform(v.begin(), v.end(), vTarg.begin(), Transform()); 35 36 for_each(vTarg.begin(), vTarg.end(), myPrint()); 37 } 38 int main() 39 { 40 test01(); 41 return 0; 42 }
2.常用查找算法
-find 查找元素
1 #include <iostream> 2 #include <vector> 3 #include <string> 4 #include <algorithm> 5 using namespace std; 6 /* 7 find(iterator_begin,iterator_end,value)//返回迭代器 8 value 为要查找的元素 9 */ 10 class Person { 11 public: 12 Person(string name, int age) :m_Name(name), m_Age(age) {} 13 14 bool operator==(Person p)//重载== 让底层代码知道怎么比较 15 { 16 if (p.m_Age == this->m_Age && p.m_Name == this->m_Name) 17 { 18 return true; 19 } 20 else return false; 21 } 22 string m_Name; 23 int m_Age; 24 }; 25 void test01() 26 { 27 vector<int>v; 28 for (int i = 1; i < 9; i++) 29 { 30 v.push_back(i); 31 } 32 vector<int>::iterator pos = find(v.begin(), v.end(), 5); 33 34 if (pos != v.end()) 35 { 36 cout << "找到了" << *pos << endl; 37 } 38 else 39 cout << "没找到" << endl; 40 41 //自定义数据类型 42 vector<Person>vp; 43 Person p1("aaa", 38); 44 Person p2("bbb", 18); 45 Person p3("ccc", 98); 46 Person p4("ddd", 88); 47 Person p5("eee", 58); 48 49 vp.push_back(p1); 50 vp.push_back(p2); 51 vp.push_back(p3); 52 vp.push_back(p4); 53 vp.push_back(p5); 54 Person pp("aaa", 38); 55 56 vector<Person>::iterator pos2 = find(vp.begin(), vp.end(), pp); 57 58 if (pos != v.end()) 59 { 60 cout << "找到了 姓名:" << (*pos2).m_Name << " 年龄:" << (*pos2).m_Age << endl; 61 } 62 else 63 cout << "没找到" << endl; 64 } 65 int main() 66 { 67 test01(); 68 return 0; 69 }
-find_if 按条件查找元素
1 #include <iostream> 2 #include <vector> 3 #include <algorithm> 4 #include <string> 5 using namespace std; 6 class GreaterFive { 7 public: 8 bool operator()(int val) 9 { 10 return val > 5; 11 } 12 }; 13 14 class Person { 15 public: 16 Person(string name,int age):m_Name(name),m_Age(age){} 17 string m_Name; 18 int m_Age; 19 }; 20 class Age_Greater_20 { 21 public: 22 bool operator()(Person& p) 23 { 24 return p.m_Age > 20; 25 } 26 }; 27 void test01() 28 { 29 vector<int>v; 30 for (int i = 0; i < 10; i++) 31 { 32 v.push_back(i); 33 } 34 35 vector<int>::iterator pos = find_if(v.begin(), v.end(), GreaterFive()); 36 37 if (pos != v.end()) 38 { 39 cout << "找到了" << *pos << endl; 40 } 41 else 42 { 43 cout << "没找着" << endl; 44 } 45 } 46 void test02()//自定义数据类型 47 { 48 vector<Person>vp; 49 Person p1("aaa", 14); 50 Person p2("bbb", 74); 51 Person p3("vvv", 16); 52 Person p4("ccc", 18); 53 Person p5("ddd", 545); 54 55 vp.push_back(p1); 56 vp.push_back(p2); 57 vp.push_back(p3); 58 vp.push_back(p4); 59 vp.push_back(p5); 60 61 vector<Person>::iterator pos = find_if(vp.begin(), vp.end(), Age_Greater_20()); 62 63 while (pos != vp.end()) 64 { 65 cout << "姓名:" << (*pos).m_Name << " " << "年龄:" << (*pos).m_Age << endl; 66 pos = find_if(pos+1, vp.end(), Age_Greater_20()); 67 } 68 } 69 int main() 70 { 71 test01(); 72 cout << "------------------------------------------" << endl; 73 test02(); 74 return 0; 75 }find_if
-adjacent_find 查找相邻重复元素
1 #include <iostream> 2 #include <vector> 3 #include <algorithm> 4 using namespace std; 5 /* 6 * 查找相邻重复元素,找到了会返回重复的第一个位置 7 adjacent_find(v.begin(),v.end()); 8 */ 9 void test01() 10 { 11 vector<int>v; 12 v.push_back(2); 13 v.push_back(8); 14 v.push_back(6); 15 v.push_back(4); 16 v.push_back(5); 17 v.push_back(5); 18 19 vector<int>::iterator pos = adjacent_find(v.begin(), v.end()); 20 21 if (pos == v.end()) 22 { 23 cout << "未找到相邻元素" << endl; 24 } 25 else 26 cout << "找到相邻元素" << *pos << endl; 27 } 28 int main() 29 { 30 test01(); 31 return 0; 32 }
-binary_search 二分查找法
1 #include <iostream> 2 #include <vector> 3 #include <algorithm> 4 using namespace std; 5 /* 6 二分法查找:效率很高,但是必须得有顺序的才能用 7 bool binary_search(v.begin(),v.end(),value); 8 */ 9 void test01() 10 { 11 vector<int>v; 12 for (int i = 0; i < 10; i++) 13 { 14 v.push_back(i); 15 } 16 17 bool _if = binary_search(v.begin(), v.end(), 5); 18 19 if (_if) 20 { 21 cout << "找到了" << endl; 22 } 23 else cout << "没找到" << endl; 24 } 25 int main() 26 { 27 test01(); 28 return 0; 29 }
-count 统计元素的个数
1 #include <iostream> 2 using namespace std; 3 #include <string> 4 #include <vector> 5 #include <algorithm> 6 /* 7 count(iterator_begin,iterator_end);返回一个int类型的数据 8 */ 9 class Person { 10 public: 11 Person(string name, int age) :m_Name(name), m_Age(age) {} 12 string m_Name; 13 int m_Age; 14 bool operator==(Person p) 15 { 16 return p.m_Age == this->m_Age; 17 } 18 }; 19 void test01() 20 { 21 vector<int>v; 22 v.push_back(5); 23 v.push_back(15); 24 v.push_back(5); 25 v.push_back(45); 26 v.push_back(53); 27 v.push_back(3); 28 v.push_back(13); 29 v.push_back(53); 30 v.push_back(54); 31 v.push_back(54); 32 33 int num = count(v.begin(), v.end(), 53); 34 35 cout << "vector里53有" << num << "个" << endl; 36 } 37 void test02()//自定义数据类型 38 { 39 vector<Person>vp; 40 Person p1("张三", 48); 41 Person p2("李四", 25); 42 Person p3("王五", 56); 43 Person p4("麻子", 14); 44 Person p5("找死", 25); 45 46 Person pp("诸葛亮", 25); 47 vp.push_back(p1); 48 vp.push_back(p2); 49 vp.push_back(p3); 50 vp.push_back(p4); 51 vp.push_back(p5); 52 53 int num = count(vp.begin(), vp.end(), pp); 54 55 cout << "有" << num << "个人和诸葛亮年龄相同" << endl; 56 } 57 int main() 58 { 59 test01(); 60 cout << "-------------------------------------" << endl; 61 test02(); 62 return 0; 63 }
-count_if 按条件统计元素个数
1 #include <iostream> 2 #include <vector> 3 #include <algorithm> 4 #include <string> 5 using namespace std; 6 /* 7 count_if(iterator begin,iterator end,_Pred); 8 */ 9 class Greater_20 { 10 public: 11 bool operator()(int val) 12 { 13 return val > 20; 14 } 15 }; 16 class Person { 17 public: 18 Person(string name, int age) :name(name), age(age){} 19 bool operator()(Person p) 20 { 21 return p.age > 20; 22 } 23 string name; 24 int age; 25 }; 26 class Age_Greater_20 { 27 public: 28 bool operator()(Person p) 29 { 30 return p.age > 20; 31 } 32 }; 33 void test01() 34 { 35 vector<int>v; 36 for (int i = 1; i < 1000; i++) 37 { 38 v.push_back(i); 39 } 40 41 int num = count_if(v.begin(), v.end(), Greater_20()); 42 cout << "大于20的数有" << num << endl; 43 } 44 void test02()//自定义数据类型 45 { 46 vector<Person>vp; 47 Person p1("张三", 56); 48 Person p2("lishi", 45); 49 Person p3("alan", 45); 50 Person p4("wang", 65); 51 Person p5("yu", 85); 52 Person p6("ling", 45); 53 54 vp.push_back(p1); 55 vp.push_back(p2); 56 vp.push_back(p3); 57 vp.push_back(p4); 58 vp.push_back(p5); 59 vp.push_back(p6); 60 61 //int num = count_if(vp.begin(), vp.end(), greater<Person>()); 62 int num = count_if(vp.begin(), vp.end(), Age_Greater_20()); 63 64 cout << "年龄大于二十的人数有" << num << endl; 65 } 66 int main() 67 { 68 test01(); 69 cout << "=============================================" << endl; 70 test02(); 71 return 0; 72 }count_if
3.常用的排序算法
-sort //对容器的元素进行排序
-random_shuffle //洗牌 指定范围内的元素随即调整次序
-merge //容器元素合并,并存储到另一个容器中
-reverse //反转指定范围中的元素
4.常用拷贝和替换算法
-copy //容器内指定范围的元素拷贝到另一个容器中
-replace //将容器内指定范围内的元素改为新元素
-repalce_if //容器内范围内按要求替换新元素
-swap //互换两个容器的元素
5.常用集合算法
-set_intersection //求两个容器的交集
-set_union //求两个容器的并集
-set_difference //求两个容器的差集