算法主要包含的头文件有
#include<functional> #include<algorithm> #include<numeric>
#include<algorithm>这个是最大的,常用的基本都在这里
常用遍历算法
for_each遍历 transform搬移:transform (beg1 , end1 , beg2, func)
for_each
void print(int val)
{
cout << val << " ";
}
//仿函数
class compare
{
public:
void operator()(int val)
{
cout << val << " ";
}
};
void test01()
{
vector<int> v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
for_each(v.begin(), v.end() , print);
cout << endl;
for_each(v.begin(), v.end(), compare());
}
//上述两种方法都可以 可以使用普通函数也可以使用仿函数
transform
class transform1
{
public:
int operator()(int a )
{
return a;
}
};
class Myprint
{
public :
void operator()(int val)
{
cout << val << " ";
}
};
void test02()
{
vector<int> v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
vector<int> vtarget;
vtarget.resize(20);//注意
transform(v.begin(), v.end(), vtarget.begin(), transform1());//直接运行会报错,原因是新的vtarger中没有大小,要先开辟容量
for_each(vtarget.begin(), vtarget.end(), Myprint());
cout << endl;
}
在transform中,需要传入的第四个参数是回调函数,回调函数决定了如何对输入元素进行转换。
比如上述代码中使用的回调函数,就是将原容器中的数据一样的迁移到新的容器中,如果想要让原容器中的元素都+10迁移到新容器中,就可以改变回调函数。
class Mytransform
{
public:
int operator()(int a )
{
return a+10;
}
};
查找算法
返回指定元素迭代器 ,找不到返回end迭代器
find find_if adjacent_find(查找相邻重复) binary_search(二分法) count count_if 计数
这里如果是内置数据类型的话就比较简单,使用和上述基本一致,下面来看一下自定义数据类型的时候的做法。
find 、 find_if 、 adjacent_find 返回的都是迭代器,要用迭代器接收。
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_age == p.m_age && this->m_name == p.m_name )
{
return true;
}
else
{
return false;
}
}
string m_name ;
int m_age;
};
void test01()
{
Person p1("Tom", 15);
Person p2("gui", 12);
Person p3("erzi", 34);
Person p4("Amy", 23);
Person p5("Sam", 54);
vector<Person> v;
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
v.push_back(p5);
Person p("Amy" , 23);
vector<Person>::iterator it = find(v.begin() , v.end() , p);
if(it != v.end())
{
cout << "未找到" << endl;
}
else
{
cout << "找到p2" << "姓名 : "<<(*it).m_name <<" 年龄: "<< (*it).m_age<< endl;
}
}
主要的用途就是查找到容器中是否含有相同的人 。 由于这边使用的是自定义数据类型 , 所以当没有重载的时候,系统底层不知道如何进行==操作,所以要重载==运算符。
find_if
find_if(begin , end , _Pred)_Pred:_Pred函数或者谓词 返回bool类型的仿函数
找到相应条件的。
class Person
{
public:
Person(string name , int age )
{
this->m_name = name ;
this->m_age = age ;
}
string m_name ;
int m_age ;
};
class Mycompare
{
public:
bool operator()(const Person&p)
{
return p.m_age > 20;
}
};
void test06()
{
Person p1("Tom", 15);
Person p2("gui", 12);
Person p3("erzi", 34);
Person p4("Amy", 23);
Person p5("Sam", 54);
vector<Person> v;
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
v.push_back(p5);
vector<Person>::iterator it = find_if(v.begin(), v.end(), Mycompare());
if (it == v.end())//
{
cout << "未找到" << endl;
}
else
{
cout << "找到 " << "姓名 : " << (*it).m_name << " 年龄: " << (*it).m_age << endl;
}
}
adjacent_find
adjacentf_find(beg , end ) 查找相邻重复元素,返回相邻元素的第一个位置迭代器
binary_search
查找指定元素是否存在
binary_search(beg , end ,value) 返回bool类型
注意: 必须要在有序的数列里面查找
count
count(begin , end , value) 计数
count_if
count_if(beg , end , _Pred)