文章目录

  • 一、基本概念
  • 二、程序示例
    • 1.遍历
    • 2. 查找
    • 3. 排序、拷贝、替换
    • 4. numeric相关算法
  • 总结

一、基本概念

算法是STL中很重要的一部分,其功能包括比较,查找,排序,交换,遍历,复制等等。

最大的算法头文件是algorithm,封装了很多种模板类。还有numeric和functional也比较常见。

二、程序示例

1.遍历

#include<iostream>
#include<list>
#include<algorithm>
using namespace std;void print(int a)
{cout << a << " ";
}class print1
{public:void operator()(int a){cout << a << " ";}
};class print2
{public:int operator()(int a){cout << a << " ";return a;}
};void test()
{list<int>L;L.push_back(1);L.push_back(2);L.push_back(3);//函数作为形参进行遍历输出for_each(L.begin(), L.end(), print);cout << endl;//仿函数进行遍历for_each(L.begin(), L.end(), print1());cout << endl;//transform实现遍历list<int>L1;L1.resize(L.size());transform(L.begin(),L.end(),L1.begin(), print2());}int main()
{test();system("pause");
}

2. 查找

#include<iostream>
#include<list>
#include<algorithm>
using namespace std;class Compare
{public://一元谓词bool operator()(int a){return a > 1;}
};class Cat
{public:Cat(string name, int color, int age){this->Name = name;this->Color = color;this->Age = age;}//自定义数据类型需要重载==bool operator==(const Cat& cat){if (Name == cat.Name && Color == cat.Color && Age == cat.Age){return true;}else{return false;}}public:string Name;int Color;int Age;
};class print1
{public:bool operator()(Cat& cat){return cat.Age > 3;}
};void test()
{list<int>L;L.push_back(1);L.push_back(2);L.push_back(3);//find算法查找list<int>::iterator i = find(L.begin(), L.end(), 1);if (i == L.end()){cout << "未查找到" << endl;}else{cout << "查找到" << *i<<endl;}//find_if查找list<int>::iterator i1 = find_if(L.begin(),L.end(),Compare());if (i1 == L.end()){cout << "未查找到" << endl;}else{cout << "查找到" << *i1 << endl;}//查找元素是否存在,无序序列结果未知bool i2 = binary_search(L.begin(), L.end(), 1);if (i2){cout << "查找到1" << endl;}else{cout << "未查找到" << endl;}//count统计int n = count(L.begin(), L.end(),1);cout << n << endl;//count_if统计int n1 = count_if(L.begin(), L.end(), Compare());cout << n1 << endl;}void test1()
{list<Cat>L;Cat cat1("小100", 76, 2);Cat cat2("小200", 32, 2);Cat cat3("小300", 32, 4);Cat cat4("小400", 32, 3);Cat cat5("小500", 54, 1);//插入L.push_back(cat1);L.push_back(cat2);L.push_back(cat3);L.push_back(cat4);L.push_back(cat5);//find查找list<Cat>::iterator i = find(L.begin(), L.end(), cat1);if (i == L.end()){cout << "未查找到" << endl;}else{cout << "查找到" << (*i).Name << endl;}//find_if查找list<Cat>::iterator i1 = find_if(L.begin(), L.end(), print1());if (i1 == L.end()){cout << "未查找到" << endl;}else{cout << "查找到" << (*i1).Name << endl;}//查找相邻的重复元素list<Cat>::iterator i2 = adjacent_find(L.begin(), L.end());if (i2 == L.end()){cout << "未查找到" << endl;}else{cout << "查找到" << (*i2).Name << endl;}Cat cat6("小300", 32, 4);//count统计,需要重载int n = count(L.begin(), L.end(), cat6);cout << n << endl;
}int main()
{test1();system("pause");
}

3. 排序、拷贝、替换

#include<iostream>
#include<vector>
#include<algorithm>
#include<ctime>
using namespace std;class Compare
{public://一元谓词bool operator()(int a,int b){return a > b;}
};class Compare3
{public://一元谓词bool operator()(int a){return a > 3;}
};void print(int a)
{cout << a << " ";
}class Cat
{public:Cat(string name, int color, int age){this->Name = name;this->Color = color;this->Age = age;}//自定义数据类型需要重载==bool operator==(const Cat& cat){if (Name == cat.Name && Color == cat.Color && Age == cat.Age){return true;}else{return false;}}public:string Name;int Color;int Age;
};void test()
{vector<int>L;L.push_back(1);L.push_back(2);L.push_back(3);L.push_back(4);//降序sort(L.begin(), L.end(),Compare());for_each(L.begin(), L.end(), print);cout << endl;//greater<int>()sort(L.begin(), L.end(),greater<int>());for_each(L.begin(), L.end(), print);cout << endl;sort(L.begin(), L.end());//随机打乱//srand((unsigned int)time(NULL));//random_shuffle(L.begin(), L.end());//for_each(L.begin(), L.end(), print);//cout << endl;vector<int>L1(L);vector<int>L2;L2.resize(L.size()+L1.size());//合并,默认只能同为升序的合并merge必须为有序序列merge(L.begin(), L.end(), L1.begin(), L1.end(),L2.begin());for_each(L2.begin(), L2.end(), print);cout << endl;//反转reverse(L.begin(), L.end());for_each(L.begin(), L.end(), print);cout << endl;//拷贝vector<int>L3;L3.resize(L.size());copy(L.begin(), L.end(), L3.begin());for_each(L3.begin(), L3.end(), print);cout << endl;//替换replace(L.begin(), L.end(), 2, 5);for_each(L.begin(), L.end(), print);cout << endl;replace_if(L.begin(), L.end(), Compare3(),20);for_each(L.begin(), L.end(), print);cout << endl;//互换swap(L, L1);
}int main()
{test();system("pause");
}

4. numeric相关算法

#include<iostream>
#include<vector>
#include<numeric>
#include<algorithm>
#include<ctime>
using namespace std;void print(int a)
{cout << a << " ";
}void test()
{vector<int>L;L.push_back(1);L.push_back(2);L.push_back(3);L.push_back(4);//计算容器元素的和,0为起始累加值int total = accumulate(L.begin(), L.end(), 0);cout << total<<endl;//填充元素vector<int>L1;L1.resize(L.size());fill(L1.begin(), L1.end(), 3);for_each(L1.begin(), L1.end(), print);cout << endl;//求交集vector<int>L2;L2.resize(min(L.size(),L1.size()));vector<int>::iterator i = set_intersection(L.begin(), L.end(), L1.begin(), L1.end(), L2.begin());for_each(L2.begin(), i, print);cout << endl;//求并集vector<int>L3;L3.resize(L.size()+ L1.size());vector<int>::iterator j = set_union(L.begin(), L.end(), L1.begin(), L1.end(), L3.begin());for_each(L3.begin(), j, print);cout << endl;//差集vector<int>L4;L4.resize(max(L.size() , L1.size()));vector<int>::iterator j1 = set_difference(L.begin(), L.end(), L1.begin(), L1.end(), L4.begin());for_each(L4.begin(), j1, print);
}int main()
{test();system("pause");
}

总结

以上只是stl算法中常见的,后续会随时补充新的算法。

C++STL总结笔记(三)—— 常见算法相关推荐

  1. 2018-3-22论文一种新型的智能算法--狼群算法(笔记三)算法的步骤+收敛性分析

    首先整体的系统: 来源:[图文]狼群智能算法简述_百度文库 https://wenku.baidu.com/view/e4f45d6c04a1b0717fd5ddaf.html 算法的步骤: 公式:上 ...

  2. 学习笔记(三) 线性回归算法(Linear Regression)

    一.算法简介 代码下载:https://download.csdn.net/download/xdg2008/14017829 1.1 什么是回归分析 回归分析是一种预测性的建模技术,它研究的是因变量 ...

  3. 机器学习笔记(常见算法)

    C4.5 属于决策树算法,既可以解决分类问题,也可以解决回归问题,属于有监督算法. 现在基本不用这个算法了. K-Means 属于聚类算法,属于无监督算法 SVM(*) 支持向量机. 曾经一度认为是分 ...

  4. c++学习笔记三-常见基础问题

    1.windows系统下标准输入的fd是多少? coder: windows下console没有所谓的0,1,2标准输入输出fd这样的概念.windows下的进程创建时默认是没有这些输入输出的,可以在 ...

  5. C++ STL学习笔记

    C++ STL学习笔记一 为何要学习STL: 数据结构与算法是编程的核心,STL中包含各种数据结构和优秀的算法,确实值得深入学习,本文中虽然着重使用,但希望有心的朋友能多看看相关数据结构的实现,对于C ...

  6. 【笔记】三张图读懂机器学习:基本概念、五大流派与九种常见算法

    文章目录 [笔记]三张图读懂机器学习:基本概念.五大流派与九种常见算法 Chapter 1: A look at Machine learning 1.What is it? 2.How does m ...

  7. 可由一个尾指针唯一确定的链表有_极客算法训练笔记(三),链表详细图解,别再逃避了朋友...

    目录 缓存引爆链表 链表单链表双向链表循环链表双向循环链表 LinkedHashMap实现LRU缓存,源码解析(JDK1.8) 算法 爬楼梯 算法 反转链表 算法 链表环检测 缓存引爆链表 存储结构 ...

  8. 严蔚敏算法约瑟夫环_极客算法训练笔记(三),链表详细图解,别再逃避了朋友...

    目录 缓存引爆链表 链表 单链表 双向链表 循环链表 双向循环链表 LinkedHashMap实现LRU缓存,源码解析(JDK1.8) 算法 爬楼梯 算法 反转链表 算法 链表环检测 缓存引爆链表 存 ...

  9. STL::算法::常见算法

    总述 定位 泛型编程(GP)走了一条与面向对象编程(OOP)完全不同的道路,各种容器类的设计与实现也没有走严格意义的继承.接口机制.在STL的设计与实现中,算法并非是容器类的成员函数,而是一种搭配迭代 ...

  10. JavaSE基础笔记——常用API、Lambda、常见算法

    日期与时间 时间日期是在任何一个程序系统里几乎都不可能忽略掉的数据量,而且大量的算法在底层都会使用到时间日期数据值作为算法的基本种子(随机数算法或加密算法都经常用到). 计算机里,时间日期的本质 作为 ...

最新文章

  1. hdu3768 spfa+全排列
  2. NF5270M3服务器主板安装系统,NF5270M3 – 主板相关
  3. c# 在DataTable的第一列(指定列)的前面添加一列
  4. php实现数值的整数次方
  5. OllyDBG 入门系列(二)-字串参考
  6. Gprinter Android SDK V2.1 使用说明
  7. CMMI5 2.0版本是什么 做什么
  8. opencv之调取摄像头拍照
  9. 程序员工作中的一些建议
  10. ImageNet-1k分类数据集中英对照表 验证集类别解析
  11. 自大型人格分析,如何改变自大型性格?
  12. 基于拉丁超立方抽样的风,光,负荷场景生成方法 风电功率场景生成 ,光伏功率场景生成,负荷场景生成
  13. 桌面的「微信」坏了,「如何恢复」
  14. 【数据说第二期】联盟第一人詹姆斯四个时期大PK
  15. ps cc 生成html,ps cc中怎么生成图像资源?
  16. 【原创】解决windows命令行运行程序必须输入.exe后缀的问题
  17. United Cows of Farmer John G
  18. 软件工程导论——需求分析总结
  19. C#实现多语言切换详细教程(附源码)
  20. 北大青鸟java y2_北大青鸟Y2Java3个月分结业测试题 包含源码

热门文章

  1. P3865 【模板】ST表
  2. SMMS 2016 啟用深色主題
  3. javascript之变量
  4. Django 学习资源
  5. asp.net url传值,弹窗
  6. 12306订票助手更新
  7. Android源码下载(ubuntu12.04(amd64))
  8. 【未解决】Reporting Services报表在浏览器中的显示
  9. Uncaught TypeError: Cannot redefine property: $router
  10. 2.数据结构笔记学习--线性表基本操作