算法概述

  1. 算法主要由头文件组成
  2. 是所有STL头文件中最大的一个,其中常用的功能涉及到比较,交换,查找,遍历,复制,修改,反转,排序,合并等
  3. 体积很小,只包括在几个序列容器上进行的简单运算的模板函数
  4. 定义一些模板类,用以声明函数对象

遍历算法

for_each()

  1. for_each: 用指定函数依次对指定范围内所有元素进行迭代访问。该函数不得修改 序列中的元素。

for_each基础遍历

#include<iostream>using namespace std;
#include<vector>
#include<algorithm>
#include<functional>
//回调函数
//void myPrint(int v)
//{
//  cout << v << endl;
//}//仿函数
struct myPrint01
{
public:void operator()(int v){cout << v << endl;}
};void test01()
{vector<int>v;for (int i = 0; i < 10; i++){v.push_back(i);}for_each(v.begin(), v.end(), myPrint01());}

for_each可以保存内部记录,有返回值

struct myPrint02
{
public:void operator()(int v){cout << v << endl;this->m_Count++;}int m_Count;
};
void test02()
{vector<int>v;for (int i = 0; i < 10; i++){v.push_back(i);}myPrint02 print2 = for_each(v.begin(), v.end(), myPrint02());cout << print2.m_Count << endl;
}

for_each可以绑定参数进行输出

struct myPrint03:public binary_function <int,int,void>
{
public:void operator()(int v,int start) const{cout << v+start << endl;}
};void test03()
{vector<int>v;for (int i = 0; i < 10; i++){v.push_back(i);}for_each(v.begin(), v.end(), bind2nd(myPrint03(), 10000));
}

transform()

  1. transform: 与 for_each 类似,遍历所有元素,但可对容器的元素进行修改
  2. 注意,transform 不会给目标容器分配内存,所以需要我们提前分配好内存

transform基础遍历

class TransForm
{
public:int operator()(int val){return val+10;}
};
void test04()
{vector<int>v; //原容器for (int i = 0; i < 10; i++){v.push_back(i);}vector<int>vTarget; //目标容器vTarget.resize(v.size());//开辟空间transform(v.begin(), v.end(), vTarget.begin(), TransForm());for_each(vTarget.begin(), vTarget.end(), [](int val){cout << val << endl; });
}

transform第二种用法,把两个容器搬到第三个容器中

class TransForm2
{
public:int operator()(int val,int val2){return val + val2;}
};void test05()
{vector<int>v1;vector<int>v2;for (int i = 0; i < 10; i++){v1.push_back(100 + i);v2.push_back(200 + i);}vector<int>vTarget;//目标容器vTarget.resize(v1.size());//提供空间transform(v1.begin(), v1.end(), v2.begin(), vTarget.begin(), TransForm2());for_each(vTarget.begin(), vTarget.end(), [](int val){cout << val << endl; });
}

查找算法

adjacent_find()

在 iterator 对标识元素范围内,查找一对相邻重复元素,找到则返回指向这对元素的第 一个元素的迭代器。否则返回 past-the-end。

void test04()
{vector<int>v;v.push_back(2);v.push_back(3);v.push_back(4);v.push_back(5);v.push_back(5);v.push_back(6);v.push_back(2);vector<int>::iterator pos=adjacent_find(v.begin(), v.end());if (pos != v.end()){cout << "找到了相邻元素" << *pos << endl;}else{cout << "未找到" << endl;}
}

binary_search

在有序序列中查找 value,找到则返回 true。注意:在无序序列中,不可使用。

void test05()
{vector<int>v;for (int i = 0; i < 10; i++){v.push_back(i);}bool ret=binary_search(v.begin(), v.end(), 4);if (ret){cout << "找到了" << endl;}else{cout << "未找到" << endl;}
}

count()

利用等于操作符,把标志范围内的元素与输入值比较,返回相等的个数。

count_if()

按条件进行比较

class GreateThenFour
{
public:bool operator()(int v){return v >=4;}
};
void test06()
{vector<int>v;for (int i = 0; i < 10; i++){v.push_back(i);}v.push_back(4);v.push_back(4);v.push_back(4);v.push_back(4);int num=count(v.begin(), v.end(), 4);cout << "4的个数为:" << num << endl;num =count_if(v.begin(), v.end(), GreateThenFour());cout << "大于等于4的个数为:" << num << endl;
}

find()

  1. find: 利用底层元素的等于操作符,对指定范围内的元素与输入值进行比较。当匹 配时,结束搜索,返回该元素的迭代器。

  2. equal_range: 返 回 一 对 iterator , 第 一 个 表 示 lower_bound, 第 二 个 表 示 upper_bound。

    void test01()
    {vector<int>v;for (int i = 0; i < 10; i++){v.push_back(i);}find(v.begin(), v.end(), 5);//查找这个容器中有没有5vector<int>::iterator pos = find(v.begin(), v.end(), 5);if (pos != v.end()){cout << "找到了数据:" << *pos << endl;}else{cout << "没找到" << endl;}
    }
    

利用find查找自定义数据类型

class Person
{
public:Person(string name, int age){this->m_Name = name;this->m_Age = age;}bool operator==(const Person&p){if (this->m_Name ==p.m_Name&&this->m_Age == p.m_Age){return true;}return false;}string m_Name;int m_Age;
};
void test02()
{vector<Person>v;Person p1("aaa", 10);Person p2("bbb", 20);Person p3("ccc", 30);Person p4("ddd", 40);v.push_back(p1);v.push_back(p2);v.push_back(p3);v.push_back(p4);vector<Person>::iterator pos= find(v.begin(), v.end(), p2);if (pos != v.end()){cout << "找到了数据姓名:" << (*pos).m_Name<<"年龄:"<<pos->m_Age << endl;}else{cout << "没找到" << endl;}
}

find_if()

find_if: 使用输入的函数代替等于操作符执行 find。返回被找到的元素的迭代器。 假设 vectorvecIntA,vecIntA 包含 1,3,5,3,9 元素

//find_if
class MyCompare:public binary_function<Person*,Person*,bool>
{
public:bool operator ()(Person *p1,Person *p2)const{if (p1->m_Name==p2->m_Name&&p1->m_Age==p2->m_Age){return true;}return false;}
};
void test03()
{vector<Person *>v;Person p1("aaa", 10);Person p2("bbb", 20);Person p3("ccc", 30);Person p4("ddd", 40);v.push_back(&p1);v.push_back(&p2);v.push_back(&p3);v.push_back(&p4);Person *p = new Person("bbb", 21);vector<Person *>::iterator pos= find_if(v.begin(), v.end(),bind2nd(MyCompare(),p));if (pos != v.end()){cout << "找到了数据姓名:" << (*pos)->m_Name << "年龄:" << (*pos)->m_Age << endl;}else{cout << "没找到" << endl;}
}

c++中STL的常用算法---2(遍历算法,查找算法)相关推荐

  1. morris算法(莫里斯遍历) [数据结构与算法]

    morris算法(莫里斯算法) Morris算法, 我们最常用于解决二叉树的遍历问题, 以及与遍历相关的一些问题(其实在二叉树系列问题中, 我们解决各种问题的时候都要涉及到一个对二叉树的遍历) 我们先 ...

  2. 家族关系查询系统程序设计算法思路_七大查找算法(附C语言代码实现)

    来自:Poll的笔记 - 博客园 链接:http://www.cnblogs.com/maybe2030/p/4715035.html 阅读目录 1.顺序查找 2.二分查找 3.插值查找 4.斐波那契 ...

  3. 大数据与算法系列之海量数据查找算法

    在某些时候,可能会涉及在海量数据中的查找,如果采用通常的做法,则很难达到一定的效果,在实际工程实践中,海量数据的查找性能很肯恩鬼成为整个系统的性能瓶颈,在海量数据中的查找包括基于布隆过滤器的方式,以及 ...

  4. python二分法查找算法_顺序查找算法和折半(二分法)查找算法,C语言查找算法详解...

    查找是指在大量的信息中寻找一个特定的信息.在计算机中,查找是非常重要的一个应用,比如"百度".查找算法的好坏直接影响查找的速度. 常用的查找算法主要有顺序查找和折半(二分法)查找: ...

  5. c++中STL的常用算法--1(函数对象,谓词,内建函数对象)

    函数对象 重载函数调用操作符的类,其对象常称为函数对象(function object),即它们是行为类似函数的对象,也叫仿函数(functor),其实就是重载"()"操作符,使得 ...

  6. 【数据结构与算法】比较法分析查找算法与查找结构

    基本的查找技术: 线性表的查找技术 顺序查找 分块查找 二分查找(折半查找) 插值查找 树表的查找技术 二叉排序树 平衡二叉树 B树(B+树.B-树等) 散列表的查找技术 开散列表 闭散列表 顺序查找 ...

  7. python实现二分查找算法_python实现二分查找算法

    ??二分算法的定义不在多说了,百度一下就知道(支持国产 ) import syssource = [1,2,3,4,5,6,7,8,9,10] #must be in orderdes = int(s ...

  8. python算法与数据结构-二分查找算法

    二分查找又称折半查找,优点是比较次数少,查找速度快,平均性能好;其缺点是要求待查表为有序表,且插入删除困难. 因此折半查找方法适用于不经常变动而查找频繁的有序列表. 递归实现二分查找,代码如下所示: ...

  9. C++中STL中的大、小、相等概念

    1.STL的大.小.相等概念 STL中关联容器内部的元素是排序的.STL中的许多算法也涉及排序.查找.这些容器和算法都需要对元素进行比较,有的比较是否相等,有的比较元素大小. 在STL中,默认情况下, ...

最新文章

  1. armv8/armv9的简介-学习这一篇就够了
  2. Qt小传——从诞生到发展、繁荣
  3. 10 种机器学习算法的要点(附 Python 和 R 代码)
  4. java垃圾回收到老年代次数,Java垃圾回收之回收算法
  5. java高级教程_高级Java教程
  6. 界面无小事(八):RecyclerView增删item
  7. hql分页获取数据总数_最简洁的分页插件PageHelper
  8. php swoole多进程,PHP基于swoole多进程操作示例
  9. 01-windows下python爬取网页上的图片
  10. SpringBoot时间格式化
  11. docker 安装依赖_史上最全Docker环境安装指南-让安装docker简单到爆
  12. JSON: jasckson 字段 过滤
  13. Image Pyramids
  14. ESP实验02-读取DS1307eerom34c32
  15. 100 行 js 代码下载抖音无水印视频
  16. 监督学习的基本假设——联合概率分布,独立同分布
  17. html图片幻灯片效果,使用CSS3实现的超酷幻灯图片效果
  18. Vast.ai GPU服务器连接
  19. 使用vue-router却导致页面空白无法呈现-报错?
  20. java 内部接口 内部类_Java接口/内部类

热门文章

  1. 用C++调用tensorflow在python下训练好的模型(centos7)
  2. 一个edit的学习笔记
  3. 介绍“Razor”— ASP.NET的一个新视图引擎
  4. 【7】jQuery学习——入门jQuery选择器之过滤选择器-可见性过滤选择器
  5. PHP文件系统-文件下载
  6. visio生成数据表图
  7. MyGeneration的NHibernate代码生成模版
  8. plsql 为空显示 0 的函数_不加班只加薪!从0到1教你制作出入库进销存表格
  9. jquery ajax.then,jQuery动态AJAX Promise链
  10. python编程制作接金币游戏_pygame学习笔记(6):完成一个简单的游戏