• STL常用遍历,查找,算法


    目录

    1.遍历算法

     1.1for_earch

    1.2transform

    2.常用查找算法

    2.1find,返回值是迭代器

    2.1.1查找内置数据类型 

     2.1.2查找自定义数据类型

    2.2fin_if 按条件查找元素

    2.2.1查找内置的数据类型

    2.2.2查找内置数据类型

    2.3查找相邻元素adjeacent_find

    2.4查找指定元素是否存在binarary_search

    2.5统计元素的个数count

     2.5.1统计内置数据类型

    2.5.2统计自定义数据类型

    2.6按条件统计元素个数

    2.6.1统计内置数据类型 

    2.6.2统计自定义的数据类型

    3.常用排序算法

    3.1sort


    1.遍历算法

     1.1for_earch

    1. #include
    2. using namespace std;
    3. #include
    4. #include
    5. //常用遍历算法 for_each
    6. //利用普通函数实现
    7. void print01(int val)
    8. {
    9. cout << val << " ";
    10. }
    11. //仿函数(函数对象)本身是个类。不是一个函数
    12. class print02
    13. {
    14. public:
    15. void operator()(int val)
    16. {
    17. cout << val << " ";
    18. }
    19. };
    20. void test01()
    21. {
    22. vector<int>v;
    23. for (int i = 0; i < 10; i++)
    24. {
    25. v.push_back(i);
    26. }
    27. for_each(v.begin(), v.end(), print01);//第三个位置,普通函数是把函数名放过来
    28. cout << endl;
    29. for_each(v.begin(), v.end(), print02());//第三个位置需要传入函数对象
    30. //类名加小括号,创建出匿名对象
    31. }
    32. int main()
    33. {
    34. test01();
    35. system("pause");
    36. return 0;
    37. }

    1.2transform

    1. #include
    2. using namespace std;
    3. #include
    4. #include
    5. //常用遍历算法 transform
    6. //仿函数(函数对象)本身是个类。不是一个函数
    7. class Transform
    8. {
    9. public:
    10. //搬运过程中把每个元素取出来在返回回去,由于操作的是int型,所以返回int
    11. int operator()(int val)
    12. {
    13. return val+100;//+100在搬到容器中
    14. }
    15. };
    16. class Myprint
    17. {
    18. public:
    19. void operator()(int val)
    20. {
    21. cout << val << " ";
    22. }
    23. };
    24. void test01()
    25. {
    26. vector<int>v;
    27. for (int i = 0; i < 10; i++)
    28. {
    29. v.push_back(i);
    30. }
    31. vector<int>vTarget;//目标容器
    32. vTarget.resize(v.size());//目标容器 需要提前开辟空间,不然报错
    33. transform(v.begin(), v.end(), vTarget.begin(), Transform());//最后一个位置函数对象
    34. for_each(vTarget.begin(), vTarget.end(), Myprint());//最后一个位置函数对象
    35. cout << endl;
    36. }
    37. int main()
    38. {
    39. test01();
    40. system("pause");
    41. return 0;
    42. }

    2.常用查找算法

    2.1find,返回值是迭代器

    2.1.1查找内置数据类型 

    1. #include
    2. using namespace std;
    3. #include
    4. #include
    5. #include
    6. //常用查找算法
    7. //find
    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. //查找 容器中 是否有 5 这个元素
    17. vector<int>::iterator it = find(v.begin(), v.end(), 50);
    18. if (it == v.end())
    19. {
    20. cout << "没有找到!" << endl;
    21. }
    22. else
    23. {
    24. cout << "找到:" << *it << endl;
    25. }
    26. }
    27. int main()
    28. {
    29. test01();
    30. system("pause");
    31. return 0;
    32. }

     2.1.2查找自定义数据类型

    1. #include
    2. using namespace std;
    3. #include
    4. #include
    5. #include
    6. //常用查找算法
    7. //find
    8. class Person
    9. {
    10. public:
    11. Person(string name, int age)
    12. {
    13. m_Name = name;
    14. this->m_Age = age;
    15. }
    16. //重载== 让底层find知道如何对比person数据类型
    17. bool operator ==(const Person& p)//const防止修改p
    18. {
    19. if (this->m_Name == p.m_Name && this->m_Age == p.m_Age)
    20. {
    21. return true;
    22. }
    23. else
    24. {
    25. return false;
    26. }
    27. }
    28. string m_Name;
    29. int m_Age;
    30. };
    31. //查找 自定义数据类型
    32. void test02()
    33. {
    34. vectorv;
    35. //创建数据
    36. Person p1("aaa", 10);
    37. Person p2("bbb", 20);
    38. Person p3("ccc", 30);
    39. Person p4("ddd", 40);
    40. //放到容器中
    41. v.push_back(p1);
    42. v.push_back(p2);
    43. v.push_back(p3);
    44. v.push_back(p4);
    45. Person p("bbb", 20);
    46. //查找是否有和p一样的
    47. vector::iterator it = find(v.begin(), v.end(), p);
    48. if (it == v.end())
    49. {
    50. cout << "没有找到" << endl;
    51. }
    52. else
    53. {
    54. cout << "找到元素:姓名:" << (*it).m_Name << " 年龄:" << it->m_Age << endl;
    55. }
    56. }
    57. int main()
    58. {
    59. test02();
    60. system("pause");
    61. return 0;
    62. }

    2.2fin_if 按条件查找元素

    2.2.1查找内置的数据类型

    1. #include
    2. using namespace std;
    3. #include
    4. #include
    5. #include
    6. //常用查找算法
    7. //find_if
    8. //1.查找内置数据类型
    9. class GreaterFive
    10. {
    11. public://谓词返回bool
    12. bool operator()(int val)//find_if的底层也是取出每个元素并解引用,放到重载小括号里去操纵
    13. {
    14. return val > 5;//大于5 的时候就返回真
    15. }
    16. };
    17. void test01()
    18. {
    19. vector<int>v;
    20. for (int i = 0; i < 10; i++)
    21. {
    22. v.push_back(i);
    23. }
    24. //返回一个迭代器
    25. vector<int>::iterator it=find_if(v.begin(), v.end(), GreaterFive());//第三个位置是匿名函数对象
    26. if (it == v.end())
    27. {
    28. cout << "没有找到大于5的元素" << endl;
    29. }
    30. else
    31. {
    32. cout << "找到大于5的数字为:" << *it << endl;
    33. }
    34. }
    35. //2.查找自定义数据类型
    36. int main()
    37. {
    38. test01();
    39. system("pause");
    40. return 0;
    41. }

    2.2.2查找内置数据类型

    1. #include
    2. using namespace std;
    3. #include
    4. #include
    5. #include
    6. //常用查找算法
    7. //find_if
    8. //2.查找自定义数据类型
    9. class Person
    10. {
    11. public:
    12. Person(string name, int age)
    13. {
    14. m_Name = name;
    15. this->m_Age = age;
    16. }
    17. string m_Name;
    18. int m_Age;
    19. };
    20. class Great20
    21. {
    22. public:
    23. bool operator()(Person &p)//每个数据类型都是Perosn的数据类型用引用的方式传进来
    24. {
    25. return p.m_Age > 20;
    26. }
    27. };
    28. bool G2(Person& p)
    29. {
    30. return p.m_Age > 20;
    31. }
    32. void test02()
    33. {
    34. vectorv;
    35. //创建数据
    36. Person p1("aaa", 10);
    37. Person p2("bbb", 20);
    38. Person p3("ccc", 30);
    39. Person p4("ddd", 40);
    40. v.push_back(p1);
    41. v.push_back(p2);
    42. v.push_back(p3);
    43. v.push_back(p4);
    44. //找年龄大于20的人
    45. vector::iterator it = find_if(v.begin(), v.end(), Great20());
    46. if (it == v.end())
    47. {
    48. cout << "没有找到" << endl;
    49. }
    50. else
    51. {
    52. cout << "找到姓名:" << (*it).m_Name << "年龄:" << it->m_Age << endl;
    53. }
    54. vector::iterator it1 = find_if(v.begin(), v.end(), Great20());
    55. if (it1 == v.end())
    56. {
    57. cout << "没有找到" << endl;
    58. }
    59. else
    60. {
    61. cout << "找到姓名:" << (*it1).m_Name << "年龄:" << it1->m_Age << endl;
    62. }
    63. }
    64. int main()
    65. {
    66. test02();
    67. system("pause");
    68. return 0;
    69. }

    2.3查找相邻元素adjeacent_find

    1. #include
    2. using namespace std;
    3. #include
    4. #include
    5. //常用查找算法
    6. //adjacent_find
    7. void test01()
    8. {
    9. vector<int>v;
    10. v.push_back(0);
    11. v.push_back(2);
    12. v.push_back(0);
    13. v.push_back(3);
    14. v.push_back(1);
    15. v.push_back(4);
    16. v.push_back(3);
    17. v.push_back(3);
    18. vector<int>::iterator it=adjacent_find(v.begin(), v.end());
    19. if (it == v.end())
    20. {
    21. cout << "未找到相邻重复元素" << endl;
    22. }
    23. else
    24. {
    25. cout << "找到相邻重复元素:" << *it << endl;
    26. }
    27. }
    28. int main()
    29. {
    30. test01();
    31. system("pause");
    32. return 0;
    33. }

    1. #include
    2. using namespace std;
    3. #include
    4. #include
    5. //常用查找算法
    6. //binary_search 二分查找法,在无序的序列中不可以用
    7. void test01()
    8. {
    9. vector<int>v;
    10. for (int i = 0; i < 10; i++)
    11. {
    12. v.push_back(i);
    13. }
    14. //查找容器中是否有9
    15. //注意容器必须是有序的序列
    16. //如果无序结果未知
    17. bool ret = binary_search(v.begin(), v.end(), 9);
    18. if (ret)
    19. {
    20. cout << "找到了元素" << endl;
    21. }
    22. else
    23. {
    24. cout << "没找到" << endl;
    25. }
    26. }
    27. int main()
    28. {
    29. test01();
    30. system("pause");
    31. return 0;
    32. }

    2.5统计元素的个数count

     2.5.1统计内置数据类型

    1. #include
    2. using namespace std;
    3. #include
    4. #include
    5. //常用查找算法
    6. //count
    7. //1.统计内置数据类型
    8. void test01()
    9. {
    10. vector<int>v;
    11. v.push_back(10);
    12. v.push_back(40);
    13. v.push_back(30);
    14. v.push_back(40);
    15. v.push_back(20);
    16. v.push_back(40);
    17. int num=count(v.begin(), v.end(), 40);
    18. cout << "40的元素个数为:" <
    19. int num1 = count(v.begin(), v.end(), 1);
    20. cout << "1的元素个数为:" << num1 << endl;//输出0
    21. }
    22. int main()
    23. {
    24. test01();
    25. system("pause");
    26. return 0;
    27. }

    2.5.2统计自定义数据类型

    1. #include
    2. using namespace std;
    3. #include
    4. #include
    5. //常用查找算法
    6. //count
    7. //2.统计自定义数据类型
    8. class Person
    9. {
    10. public:
    11. Person(string name, int age)
    12. {
    13. m_Name = name;
    14. this->m_Age = age;
    15. }
    16. bool operator==(const Person& p)//底层要加const,
    17. {
    18. if (m_Age==p.m_Age)
    19. {
    20. return true;
    21. }
    22. else
    23. {
    24. return false;
    25. }
    26. }
    27. string m_Name;
    28. int m_Age;
    29. };
    30. void test02()
    31. {
    32. vectorv;
    33. Person p1("刘备", 35);
    34. Person p2("关羽", 35);
    35. Person p3("张飞", 35);
    36. Person p4("赵云", 30);
    37. Person p5("曹操", 40);
    38. //将人员插入到容器中
    39. v.push_back(p1);
    40. v.push_back(p2);
    41. v.push_back(p3);
    42. v.push_back(p4);
    43. v.push_back(p5);
    44. Person p("诸葛亮", 35);
    45. //统计与诸葛亮年龄相同的有几人
    46. int num = count(v.begin(), v.end(), p);
    47. cout << "和诸葛亮同岁数的人员个数为:" << num << endl;
    48. }
    49. int main()
    50. {
    51. test02();
    52. system("pause");
    53. return 0;
    54. }

    2.6按条件统计元素个数

    2.6.1统计内置数据类型 

    1. #include
    2. using namespace std;
    3. #include
    4. #include
    5. //常用查找算法
    6. //count_if
    7. //1.统计内置数据类型
    8. class Greater20
    9. {
    10. public:
    11. bool operator()(int val)
    12. {
    13. return val > 20;
    14. }
    15. };
    16. void test01()
    17. {
    18. vector<int>v;
    19. v.push_back(10);
    20. v.push_back(40);
    21. v.push_back(30);
    22. v.push_back(20);
    23. v.push_back(40);
    24. v.push_back(20);
    25. int num = count_if(v.begin(), v.end(), Greater20());
    26. cout << "大于20的元素个数为:" << num << endl;
    27. }
    28. int main()
    29. {
    30. test01();
    31. system("pause");
    32. return 0;
    33. }

    2.6.2统计自定义的数据类型

    1. #include
    2. using namespace std;
    3. #include
    4. #include
    5. //常用查找算法
    6. //count_if
    7. //2.统计自定义的数据类型
    8. class Person
    9. {
    10. public:
    11. Person(string name, int age)
    12. {
    13. m_Name = name;
    14. this->m_Age = age;
    15. }
    16. string m_Name;
    17. int m_Age;
    18. };
    19. class AgeGreater20
    20. {
    21. public:
    22. bool operator()(Person &p)
    23. {
    24. return p.m_Age > 20;
    25. }
    26. };
    27. void test02()
    28. {
    29. vectorv;
    30. Person p1("刘备", 35);
    31. Person p2("关羽", 35);
    32. Person p3("张飞", 35);
    33. Person p4("赵云", 40);
    34. Person p5("曹操", 20);
    35. v.push_back(p1);
    36. v.push_back(p2);
    37. v.push_back(p3);
    38. v.push_back(p4);
    39. v.push_back(p5);
    40. //统计 大于20岁人员个数
    41. int num = count_if(v.begin(), v.end(), AgeGreater20());
    42. cout << "大于20的元素个数为:" << num << endl;
    43. }
    44. int main()
    45. {
    46. test02();
    47. system("pause");
    48. return 0;
    49. }

    3.常用排序算法

    3.1sort

    1. #include
    2. using namespace std;
    3. #include
    4. #include
    5. //常用排序算法
    6. //sort
    7. void myPrint(int val)
    8. {
    9. cout << val << " ";
    10. }
    11. void test01()
    12. {
    13. vector<int>v;
    14. v.push_back(10);
    15. v.push_back(30);
    16. v.push_back(50);
    17. v.push_back(20);
    18. v.push_back(40);
    19. //利用sort进行升序,默认情况下升序
    20. sort(v.begin(), v.end());
    21. for_each(v.begin(), v.end(), myPrint);
    22. cout << endl;
    23. //改变为降序
    24. sort(v.begin(), v.end(), greater<int>());//greater()内建函数对象,需要包含functional头文件,编译器高的不包含functional头文件也不会出错
    25. for (int i = 0; i < v.size(); i++)
    26. {
    27. cout << v[i] << " ";
    28. }
    29. cout << endl;
    30. }
    31. int main()
    32. {
    33. test01();
    34. system("pause");
    35. return 0;
    36. }
    1. bool compare(int a,int b)
    2. {
    3. return a < b; //升序排列,如果改为return a>b,则为降序
    4. }
    5. int a[20]={2,4,1,23,5,76,0,43,24,65},i;
    6. for(i=0;i<20;i++)
    7. cout<< a[i]<< endl;
    8. sort(a,a+20,compare);
    1. #include
    2. using namespace std;
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. #include
    9. #include
    10. struct Point
    11. {
    12. int x;
    13. int y;
    14. //Point(int xx, int yy) :x(xx), y(yy) {};
    15. bool operator < (Point& p) {
    16. if (x != p.x) {
    17. return x < p.x;
    18. } else {
    19. return y < p.y;
    20. }
    21. }
    22. };
    23. int main()
    24. {
    25. vector p;
    26. p.push_back(Point{ 1,2 });
    27. p.push_back(Point{ 1,3 });
    28. Point p1;
    29. p1.x = 2;
    30. p1.y = 1;
    31. p.push_back(p1);
    32. sort(p.begin(), p.end());
    33. for (int i = 0; i < p.size(); i++) {
    34. cout << p[i].x << " " << p[i].y << endl;
    35. }
    36. /*
    37. 输出:
    38. 1 2
    39. 1 3
    40. 2 1
    41. */
    42. system("pause");
    43. return 0;
    44. }
    45. --------------------------------------------------------------------------------------
    46. struct Point
    47. {
    48. int x;
    49. int y;
    50. };
    51. bool Cmp(Point& p1, Point& p2) {
    52. if (p1.x != p2.x) {
    53. return p1.x < p2.x;
    54. } else {
    55. return p1.y < p2.y;
    56. }
    57. }
    58. int main()
    59. {
    60. vector p;
    61. p.push_back(Point{ 1,2 });
    62. p.push_back(Point{ 1,3 });
    63. Point p1;
    64. p1.x = 2;
    65. p1.y = 1;
    66. p.push_back(p1);
    67. sort(p.begin(), p.end(),Cmp);
    68. for (int i = 0; i < p.size(); i++) {
    69. cout << p[i].x << " " << p[i].y << endl;
    70. }
    71. /*
    72. 输出:
    73. 1 2
    74. 1 3
    75. 2 1
    76. */
    77. system("pause");
    78. return 0;
    79. }
    80. ----------------------------------------------------------------------------------------
    81. struct Point
    82. {
    83. int x;
    84. int y;
    85. };
    86. class cmp
    87. {
    88. public:
    89. bool operator()(Point& p1, Point& p2)const {
    90. if (p1.x != p2.x) {
    91. return p1.x < p2.x;
    92. } else {
    93. return p1.y < p2.y;
    94. }
    95. }
    96. };
    97. int main()
    98. {
    99. vector p;
    100. p.push_back(Point{ 1,2 });
    101. p.push_back(Point{ 1,3 });
    102. Point p1;
    103. p1.x = 2;
    104. p1.y = 1;
    105. p.push_back(p1);
    106. sort(p.begin(), p.end(), cmp());
    107. for (int i = 0; i < p.size(); i++) {
    108. cout << p[i].x << " " << p[i].y << endl;
    109. }
    110. /*
    111. 输出:
    112. 1 2
    113. 1 3
    114. 2 1
    115. */
    116. system("pause");
    117. return 0;
    118. }

  • 相关阅读:
    玩转MySQL:多姿多彩的SQL
    Shell基础
    Docker基础—CentOS中卸载Docker
    低/无代码行业的定义及边界,低/无代码是否是伪需求
    力扣每日一题2022-08-29简单题:重新排列数组
    Hystrix熔断器整合 - 服务熔断和服务降级
    [原创]jQuery推箱子小游戏(100关且可扩展可选关),休闲,对战,娱乐,小游戏,下载即用,兼容iPad移动端,代码注释全(附源码)
    Nacos基本学习
    达梦数据库随系统开机自动启动脚本
    设计模式--策略模式
  • 原文地址:https://blog.csdn.net/qq_37891604/article/details/131713830