算法概述
- 算法主要由头文件组成
- 是所有STL头文件中最大的一个,其中常用的功能涉及到比较,交换,查找,遍历,复制,修改,反转,排序,合并等
- 体积很小,只包括在几个序列容器上进行的简单运算的模板函数
- 定义一些模板类,用以声明函数对象
遍历算法
for_each()
- 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()
- transform: 与 for_each 类似,遍历所有元素,但可对容器的元素进行修改
- 注意,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()
-
find: 利用底层元素的等于操作符,对指定范围内的元素与输入值进行比较。当匹 配时,结束搜索,返回该元素的迭代器。
-
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;}
}