一、概述
1、算法主要是由头文件<algorithm><functional><numeric>组成
2、<algorithm>是所有STL头文件中最大的一个,范围涉及到比较、交换、查找、遍历、复制、修改等等
3、<numeric>体积很小,只包括几个在序列上面进行简单数学运算的模板模块
4、<functional>定义了一些模板类,用以声明函数对象
二、常用遍历算法
for_each(beg,end,func); 实现遍历容器
beg:开始迭代器
end:结束迭代器
func:进行排序后执行的函数,正常函数不用加(),类需要加()
void print01(int val)
{
cout << val << " ";
}
class print02
{
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(), print01);
cout << endl;
for_each(v.begin(), v.end(), print02());
cout << endl;
}
transform(beg1,end1,beg2,func) 搬运容器到另一个容器中
beg1:源容器开始迭代器
end1:源容器结束迭代器
beg2:目标容器开始迭代器
func:函数或者函数对象
使用transform时必须要提前开辟空间,否则无法正常搬用
class TransForm
{
public:
int operator()(int val)
{
return val;
}
};
class MyPrint
{
public:
void operator()(int val)
{
cout << val << " ";
}
};
void test01()
{
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(), MyPrint());
}
三、常用查找算法
find(beg,end,value); 查找元素
beg:开始迭代器
end:结束迭代器
value:查找的元素
void test01()
{
vector<int> v;
for(int i=0;i<10;i++)
{
v.push_back(i+1);
}
vector<int>::iterator it = find(v.begin(), v.end(), 5);
if(it==v.end())
{
cout << "not found\n";
}
else
{
cout << "success find,value:" << *it << endl;
}
}
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;
}
else
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 it = find(v.begin(), v.end(), p2);
if(it==v.end())
{
cout << "not found" << endl;
}
else
{
cout << "name:" << it->m_name << " age:" << it->m_age << endl;
}
}
find_if(beg,end,pred); 按条件查找元素
beg:开始迭代器
end:结束迭代器
pred:函数或者谓词
class GreaterFive
{
public:
bool operator()(int val)
{
return val > 5;
}
};
void test01()
{
vector<int> v;
for (int i = 0; i < 10;i++)
{
v.push_back(i + 1);
}
vector<int>::iterator it = find_if(v.begin(), v.end(), GreaterFive());
if(it==v.end())
{
cout << "not found\n"<<endl;
}
else
{
cout << "找到大于5的数字:" << *it << endl;
}
}
class Person
{
public:
Person(string name,int age)
{
this->m_name = name;
this->m_age = age;
}
string m_name;
int m_age;
};
class Greater20
{
public:
bool operator()(Person&p)
{
return p.m_age > 20;
}
};
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 it = find_if(v.begin(), v.end(), Greater20());
if(it==v.end())
{
cout<<"not found\n"<<endl;
}
else
{
cout << "name:" << it->m_name << " age:" << it->m_age << endl;
}
}
adjacent_find(beg,end) 朝朝相邻重复元素,返回相邻元素第一个位置的迭代器
void test01()
{
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(5);
v.push_back(2);
v.push_back(4);
v.push_back(4);
v.push_back(3);
vector<int>::iterator it = adjacent_find(v.begin(), v.end());
if(it==v.end())
{
cout << "找不到相邻重复元素" << endl;
}
else
{
cout << *it << endl;
}
}
binary_search(beg,end,value) 查找指定元素,查找 返回true,否则false
void test01()
{
vector<int> v;
for (int i = 0;i<10;i++)
{
v.push_back(i);
}
bool ret = binary_search(v.begin(), v.end(), 2);
if(ret)
{
cout << "找到了" << endl;
}
else
{
cout << "未找到" << endl;
}
}
count(beg,end,value) 统计元素出现的个数
void test01()
{
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(4);
v.push_back(5);
v.push_back(3);
v.push_back(4);
v.push_back(4);
int num = count(v.begin(), v.end(), 4);
cout << num << endl;
}
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)
{
return true;
}
else
return false;
}
string m_name;
int m_age;
};
void test02()
{
vector<Person> v;
Person p1("aaa", 35);
Person p2("bbb", 35);
Person p3("ccc", 35);
Person p4("ddd", 30);
Person p5("eee", 25);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
v.push_back(p5);
Person p("asd", 35);
int num = count(v.begin(), v.end(), p);
cout << num << endl;
}
count_if(beg,end,pred) 按条件统计元素个数
class Greater4
{
public:
bool operator()(int val)
{
return val >= 4;
}
};
void test01()
{
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(4);
v.push_back(5);
v.push_back(3);
v.push_back(4);
v.push_back(4);
int num = count_if(v.begin(), v.end(), Greater4());
cout << num << endl;
}
class Person
{
public:
Person(string name,int age)
{
this->m_name = name;
this->m_age = age;
}
string m_name;
int m_age;
};
class Ageless35
{
public:
bool operator()(const Person &p)
{
return p.m_age < 35;
}
};
void test02()
{
vector<Person> v;
Person p1("aaa", 35);
Person p2("bbb", 35);
Person p3("ccc", 35);
Person p4("ddd", 30);
Person p5("eee", 25);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
v.push_back(p5);
int num = count_if(v.begin(), v.end(), Ageless35());
cout << num << endl;
}
四、常用排序算法
sort(beg,end,pred) 查找指定元素,找到返回指定位置迭代器,找不到返回结束位置迭代器
void myprint(int val)
{
cout << val << " ";
}
void test01()
{
vector<int> v;
v.push_back(10);
v.push_back(30);
v.push_back(50);
v.push_back(40);
v.push_back(20);
sort(v.begin(), v.end());
for_each(v.begin(), v.end(), myprint);
cout << endl;
sort(v.begin(), v.end(), greater<int>());
for_each(v.begin(), v.end(), myprint);
cout << endl;
}
random_shuffle(beg,end) 指定范围内元素随机调整次序
#include <algorithm>
#include <vector>
#include <ctime>
class myprint
{
public:
void operator()(int val)
{
cout << val << " ";
}
};
void test01()
{
srand((unsigned int)time(NULL));
vector<int> v;
for (int i = 0; i < 10;i++)
{
v.push_back(i);
}
for_each(v.begin(), v.end(), myprint());
cout << endl;
random_shuffle(v.begin(), v.end());
for_each(v.begin(), v.end(), myprint());
cout << endl;
}
merge(beg1,end1,beg2,end2,beg3) 两个容器合并,并储存到另一个容器
class myprint
{
public:
void operator()(int val)
{
cout << val << " ";
}
};
void test01()
{
vector<int> v;
vector<int> v1;
for (int i = 0; i < 10;i++)
{
v.push_back(i);
v1.push_back(i + 1);
}
vector<int> vTarget;
vTarget.resize(v1.size() + v.size());
merge(v.begin(), v.end(), v1.begin(), v1.end(), vTarget.begin());
for_each(vTarget.begin(), vTarget.end(), myprint());
cout << endl;
}
reverse(beg,end); 将容器内元素进行反转
void test01()
{
vector<int> v;
for (int i = 0; i < 10;i++)
{
v.push_back(i);
}
for_each(v.begin(), v.end(), myprint());
cout << endl;
reverse(v.begin(), v.end());
for_each(v.begin(), v.end(), myprint());
}
五、常用拷贝和替换算法
copy(beg1,end1,beg2) 容器内指定范围的元素拷贝到另一容器
void test01()
{
vector<int> v;
for (int i = 0; i < 10;i++)
{
v.push_back(i);
}
vector<int> v1;
v1.resize(v.size());
copy(v.begin(), v.end(), v1.begin());
for_each(v1.begin(), v1.end(), myprint());
cout << endl;
}
replace(beg,end,value,newvalue); 将容器内指定范围的某元素替换为新元素
void test01()
{
vector<int> v;
for (int i = 0; i < 10;i++)
{
v.push_back(i*10);
}
for_each(v.begin(), v.end(), myprint());
cout << endl;
replace(v.begin(), v.end(), 20, 2000);
for_each(v.begin(), v.end(), myprint());
cout << endl;
}
replace_if(beg,end,pred,newvalue) 将区间内满足条件的 元素,替换成指定元素
class replaceGreater30
{
public:
bool operator()(int val)
{
return val >= 30;
}
};
void test01()
{
vector<int> v;
for (int i = 0; i < 10;i++)
{
v.push_back(i*10);
}
for_each(v.begin(), v.end(), myprint());
cout << endl;
replace_if(v.begin(), v.end(), replaceGreater30(), 2000);
for_each(v.begin(), v.end(), myprint());
cout << endl;
}
swap(c1,c2); 交换两个容器的元素
void test01()
{
vector<int> v;
vector<int> v1;
for (int i = 0; i < 10;i++)
{
v.push_back(i);
v1.push_back(i * 10);
}
for_each(v.begin(), v.end(), myprint());
cout << endl;
for_each(v1.begin(), v1.end(), myprint());
cout << endl;
swap(v, v1);
for_each(v.begin(), v.end(), myprint());
cout << endl;
for_each(v1.begin(), v1.end(), myprint());
cout << endl;
}
六、常用算数生成算法
头文件需要包含#include<numeric>
accunulate(beg,end,value); 计算区间内容器元素累计总和,value为起始值
void test01()
{
vector<int> v;
for (int i = 0; i < 100;i++)
{
v.push_back(i);
}
int total = accumulate(v.begin(), v.end(), 0);
cout << total << endl;
}
fill(beg,end,value) 向容器中填充指定的元素
void test01()
{
vector<int> v;
v.resize(10);
fill(v.begin(), v.end(), 100);
for_each(v.begin(), v.end(), myprint());
cout << endl;
}
七、常用集合算法
set_intersection(beg1,end1,beg2,end2,beg3) 求两个容器的交集,放入第三个容器
void test01()
{
vector<int> v;
vector<int> v1;
for (int i = 0; i < 10;i++)
{
v.push_back(i);
v1.push_back(i + 5);
}
vector<int> vTarget;
vTarget.resize(min(v.size(), v1.size()));
vector<int>::iterator itEnd =
set_intersection(v.begin(), v.end(), v1.begin(), v1.end(), vTarget.begin());
for_each(vTarget.begin(), itEnd, myprint());
cout << endl;
}
set_union(beg1,end1,beg2,end2,dest) 求两个集合的并集
void test01()
{
vector<int> v;
vector<int> v1;
for (int i = 0; i < 10;i++)
{
v.push_back(i);
v1.push_back(i + 5);
}
vector<int> vTarget;
vTarget.resize(v.size()+v1.size());
vector<int>::iterator itEnd =
set_union(v.begin(), v.end(), v1.begin(), v1.end(), vTarget.begin());
for_each(vTarget.begin(), itEnd, myprint());
cout << endl;
}
set_difference(beg1,end1,beg2,end2,dest) 求两个集合的差集
void test01()
{
vector<int> v;
vector<int> v1;
for (int i = 0; i < 10;i++)
{
v.push_back(i);
v1.push_back(i + 5);
}
vector<int> vTarget;
vTarget.resize(max(v.size(),v1.size()));
vector<int>::iterator itEnd =
set_difference(v.begin(), v.end(), v1.begin(), v1.end(), vTarget.begin());
for_each(vTarget.begin(), itEnd, myprint());
cout << endl;
}