目录
1、学习目标:掌握常用的遍历算法
2、算法简介:
3、常用遍历算法
for_each(iterator beg, iterator end, _func);
//beg 开始迭代器
//end 结束迭代器
//_func 函数或者函数对象
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
//普通函数
void print01(int val)
{
cout << val << " ";
}
//仿函数
class print02
{
public:
void operator()(int val)
{
cout << val <<" ";
}
};
//常用遍历算法 for_each
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;
}
int main()
{
test01();
system("pause");
return 0;
}
transform(iterator beg1, iterator end1, iterator beg2, _func);
//beg1 源容器开始迭代器
//end1 源容器结束迭代器
//beg2 目标容器开始迭代器
//_func 函数或者函数对象? ?
可以在搬运的时候进行逻辑运算,也可以不进行逻辑运算(不进行逻辑运算时return v? 就行)
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
//常用的遍历算法 transform
class Transform
{
public:
int operator()(int v)
{
return v + 100;
}
};
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(v.begin(), v.end(), myPrint());
cout << endl;
for_each(vTarget.begin(), vTarget.end(), myPrint());
cout << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
注意:目标容器需要提前开辟空间
vTargrt.resize(v.size());
1.学习目标:掌握常用的查找算法
2.算法介绍:
3.算法示例
find(iterator beg,iterator end,value)
//beg开始迭代器
//end结束迭代器
//value查找的元素
#include<iostream>
using namespace std;
#include<algorithm>
#include<vector>
//查找 内置数据类型
void test01()
{
vector<int>v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
//查找容器中 是否有50 这个元素
vector<int>::iterator it = find(v.begin(), v.end(), 50);
if (it == v.end())
{
cout << "没有找到" << endl;
}
else
{
cout << "找到:" << *it << endl;;
}
}
class Person
{
public:
Person(string name, int age)
{
this->m_Name = name;
this->m_Age = age;
}
//重载 == 底层find知道如何对比person数据类型
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", 1);
Person p2("bbb", 2);
Person p3("ccc", 3);
Person p4("aaa", 4);
//放入容器中
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
Person pp("bbb", 2);
vector<Person>::iterator it = find(v.begin(), v.end(), pp);
if (it == v.end())
{
cout << "没有找到" << endl;
}
else
{
cout << "找到: 姓名:" << it->m_Name<<" 年龄"<<it->m_Age << endl;;
}
}
int main()
{
test01();
system("pause");
return 0;
}
总结:利用find可以在容器中找指定元素,返回值是迭代器
find_if(iterator beg, iterator end, _Pred);
//beg开始迭代器
//end结束迭代器
//_Pred函数或者谓词(返回bool类型的仿函数)
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
//find_if查找 内置数据类型
class GreatFive
{
public:
bool operator()(int val)
{
return val > 5;
}
};
void test01()
{
vector<int> v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
vector<int>::iterator it = find_if(v.begin(), v.end(), GreatFive());
if (it == v.end())
{
cout << "没有找到" << endl;
}
else
{
cout << "找到大于五的数为:" << *it << endl;
}
}
//find_if查找 自定义数据类型
class Person
{
public:
Person(string name, int age)
{
this->m_Name = name;
this->m_Age = age;
}
string m_Name;
int m_Age;
};
class Great20
{
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);
//找年龄大于20的人
vector<Person>::iterator it = find_if(v.begin(), v.end(), Great20());
if (it == v.end())
{
cout << "没有找到" << endl;
}
else
{
cout << "找到年龄大于20 : 姓名:" << it->m_Name << " 年龄:" << it->m_Age << endl;;
}
}
int main()
{
//test01();
test02();
system("pause");
return 0;
}
adjacent_find(iterator beg, iterator end);
//beg开始迭代器
//end结束迭代器
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
void test01()
{
vector<int> v;
v.push_back(0);
v.push_back(2);
v.push_back(0);
v.push_back(4);
v.push_back(1);
v.push_back(4);
v.push_back(1);
v.push_back(3);
vector<int>::iterator pos = adjacent_find(v.begin(), v.end());
if (pos == v.end())
{
cout << "未找到相邻元素" << endl;
}
else
{
cout << "找到相邻元素: "<<*pos << endl;
}
}
int main()
{
test01();
system("pause");
return 0;
}
bool binary_search(iterator beg, iterator end, value)
//注意:在无序序列中不可用
//beg开始迭代器
//end结束迭代器
//value查找的元素
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
void test01()
{
vector<int> v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
//查找容器中是否有9 元素
//v.push_back(2);//如果加入2后变无序序列,结果未知
//注意:容器必须是有序序列
bool ret = binary_search(v.begin(), v.end(), 9);
if (ret)
{
cout << "找到元素" << endl;
}
else
{
cout << "未找到元素" << endl;
}
}
int main()
{
test01();
system("pause");
return 0;
}
count(iterator beg, iterator end, value)
//beg开始迭代器
//end结束迭代器
//value统计的元素
#include<iostream>
#include<vector>
using namespace std;
#include<algorithm>
//统计 内置数据类型
void test01()
{
vector<int>v;
v.push_back(10);
v.push_back(40);
v.push_back(30);
v.push_back(40);
v.push_back(20);
v.push_back(40);
int num = count(v.begin(), v.end(), 40);
cout << "40的元素个数为: " << 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("a", 10);
Person p2("b", 20);
Person p3("c", 30);
Person p4("d", 40);
Person p5("d", 30);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
v.push_back(p5);
Person p("e", 50);//查找和p相同30岁的人
int num = count(v.begin(), v.end(), p);
if (num)
{
cout << "和e同岁的人有:" << num << endl;
}
else
{
cout << "无和e同岁的人" << endl;
}
}
int main()
{
//test01();
test02();
system("pause");
return 0;
}
coynt_if(iterator beg, iterator end, _Pred)
//beg开始迭代器
//end结束迭代器
//_Pred谓词
#include<iostream>
#include<vector>
using namespace std;
#include<algorithm>
//常用查找算法 count_if
//统计 内置数据类型
class Great30
{
public:
bool operator()(int val)
{
return val > 30;
}
};
void test01()
{
vector<int> v;
v.push_back(10);
v.push_back(40);
v.push_back(30);
v.push_back(40);
v.push_back(40);
int num = count_if(v.begin(), v.end(), Great30());
if (num)
{
cout << "大于30的数有:" << num << "个" << endl;
}
else
{
cout << "无大于30的数" << endl;
}
}
//统计 自定义数据类型
class Person
{
public:
Person(string name, int age)
{
this->m_Name = name;
this->m_Age = age;
}
string m_Name;
int m_Age;
};
class AgeGreat20
{
public:
bool operator()(const Person &p1)
{
return p1.m_Age > 20;
}
};
void test02()
{
vector<Person>v;
Person p1("aaa", 10);
Person p2("bbb", 20);
Person p3("ccc", 30);
Person p4("ddd", 40);
Person p5("eee", 30);
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(), AgeGreat20());
cout << "大于20人员的个数为:" << num << endl;
}
int main()
{
//test01();
test02();
system("pause");
return 0;
}
常用的排序算法
sort(iterator beg, iterator end, _Pred)
//beg开始迭代器
//end结束迭代器
//_Pred谓词,默认从小到大,可以改变排序规则
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
//常用排序算法 sort
void myPrint(int val)
{
cout << val << " ";
}
void test01()
{
vector<int> v;
v.push_back(10);
v.push_back(30);
v.push_back(20);
v.push_back(50);
v.push_back(40);
//利用sort进行升序排序
sort(v.begin(), v.end());
for_each(v.begin(), v.end(),myPrint);
cout << endl;
//改变 降序排序
sort(v.begin(), v.end(), greater<int>());//greater<int>()是内置函数
for_each(v.begin(), v.end(), myPrint);
cout << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
random_shuffle(iterator beg, iterator end)
//beg开始迭代器
//end结束迭代器
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
#include<ctime>
void myPrint(int val)
{
cout<<val<<" ";
}
void test01()
{
srand((unsigned)time(NULL));//随机数种子
vector<int> v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
//利用洗牌算法 打乱顺序
random_shuffle(v.begin(), v.end());
for_each( v.begin(), v.end(), myPrint);
cout << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
?总结:记得加上随机数种子srand((unsigned)time(NULL)),才能模拟真实的随机
reverse(iterator beg, iterator end);
//beg开始迭代器
//end结束迭代器
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
//常用排序算法 reverse
void myPrint(int val)
{
cout << val << " ";
}
void test01()
{
vector<int>v;
v.push_back(10);
v.push_back(20);
v.push_back(40);
v.push_back(30);
v.push_back(50);
v.push_back(80);
cout << "反转前:" << endl;
for_each(v.begin(), v.end(), myPrint);
cout << endl;
cout << "反转后:" << endl;
reverse(v.begin(),v.end());
for_each(v.begin(), v.end(), myPrint);
cout << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
void myPrint(int val)
{
cout << val << " ";
}
void test01()
{
//first 和 second 数组中各存有 1 个有序序列
int first[] = { 5,10,15,20,25 };
int second[] = { 7,17,27,37,47,57 };
//用于存储新的有序序列
vector<int> myvector(11);
//将 [first,first+5) 和 [second,second+6) 合并为 1 个有序序列,并存储到myvector容器中。
merge(first, first + 5, second, second + 6, myvector.begin());
//输出 myvector 容器中存储的元素
for_each(myvector.begin(), myvector.end(), myPrint);
cout << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
常用拷贝和替换算法
copy(iterator beg, iterator end, iterator dest)
//beg开始迭代器
//end结束迭代器
//dest目标起始迭代器
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
//常用的拷贝和替换算法 copy
void myPrint(int val)
{
cout << val << " ";
}
void test01()
{
vector<int> v1;
for (int i = 0; i < 10; i++)
{
v1.push_back(i);
}
vector<int> v2;
v2.resize(v1.size());
copy(v1.begin(), v1.end(), v2.begin());//等价于使用 v2=v1
for_each(v2.begin(), v2.end(), myPrint);
cout << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
总结:在利用copy算法进行拷贝时,目标容器记得提前开辟空间?
replace(iterator beg, iterator end, oldvalue,newvalue)
//beg开始迭代器
//end结束迭代器
//oldvalue旧元素
//newvalue新元素
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
//常用的拷贝和替换算法 replace
//仿函数
class myPrint
{
public:
void operator()(int val)
{
cout << val << " ";
}
};
void test01()
{
vector<int> v;
v.push_back(20);
v.push_back(30);
v.push_back(20);
v.push_back(50);
v.push_back(10);
v.push_back(30);
v.push_back(100);
v.push_back(30);
cout << "替换前:" << endl;
for_each(v.begin(), v.end(), myPrint());
cout << endl;
cout << "替换后:" << endl;
//将20替换为2000
replace(v.begin(), v.end(), 20, 2000);
for_each(v.begin(), v.end(), myPrint());
cout << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
总结:replace会替换区间内索引满足条件的元素
replace(iterator beg, iterator end, _Pred, newvalue)
//beg开始迭代器
//end结束迭代器
//_Pred谓词
//newvalue旧元素
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
//常用的拷贝和替换算法 replace_if
class myPrint
{
public:
void operator()(int val)
{
cout << val << " ";
}
};
class great30 //仿函数
{
public:
bool operator()(int val)
{
return val >= 30;
}
};
void test01()
{
vector<int> v;
v.push_back(20);
v.push_back(30);
v.push_back(50);
v.push_back(50);
v.push_back(30);
v.push_back(10);
v.push_back(10);
v.push_back(60);
v.push_back(10);
v.push_back(30);
cout << "替换前:" << endl;
for_each(v.begin(), v.end(), myPrint());
cout << endl;
//将大于等于30 替换为3000
replace_if(v.begin(), v.end(), great30(), 3000);
cout << "替换后:" << endl;
for_each(v.begin(), v.end(), myPrint());
cout << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
swap(container c1,container c2);
//c1容器
//c2容器
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
//常用拷贝和替换算法 swap
class myPrint
{
public:
void operator()(int val)
{
cout << val << " ";
}
};
void test01()
{
vector<int> v1;
vector<int> v2;
for (int i = 0; i < 10; i++)
{
v1.push_back(i);
v2.push_back(100 + i);
}
cout << "交换前:" << endl;
for_each(v1.begin(), v1.end(), myPrint());
cout << endl;
for_each(v2.begin(), v2.end(), myPrint());
cout << endl;
cout << "----------------------------" << endl;
cout << "交换后:" << endl;
swap(v1, v2);
for_each(v1.begin(), v1.end(), myPrint());
cout << endl;
for_each(v2.begin(), v2.end(), myPrint());
cout << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
?总结:swap交换容器时,注意交换的容器要同种类型
常用的算数生成算法
注意:算术生成算法属于小型算法,使用时包含头文件为#include<numeric>
accumulate(iterator beg, iterator end, value)
//beg开始迭代器
//end结束迭代器
//起始的累加值
#include<iostream>
#include<vector>
#include<numeric>
using namespace std;
//常用算术生成算法
int main()
{
vector<int>v;
for (int i = 0; i < 101; i++)
{
v.push_back(i);
}
int total = accumulate(v.begin(), v.end(), 0);//参数3是起始的累加值
cout << "total = " << total << endl;
system("pause");
return 0;
}
fill(iterator beg, iterator end, value)
//beg开始迭代器
//end结束迭代器
//起始的累加值
#include<iostream>
#include<vector>
#include<numeric>
#include<algorithm>
using namespace std;
void myPrint(int val)
{
cout << val << " ";
}
//常用算术生成算法 fill
void test01()
{
vector<int> v;
v.resize(10);
//后期重新填充
fill(v.begin(), v.end(), 100);
for_each(v.begin(), v.end(), myPrint);
cout << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
常用的集合算法:
功能描述:求两个容器的交集
函数原型:
set_intersection(iterator beg1, iterator end1,iterator beg2, iterator end2,?iterator dest)
//beg1容器1开始迭代器
//end1容器1结束迭代器
//beg2容器2开始迭代器
//end2容器2结束迭代器
//dest目标容器起始迭代器
注意:两个集合必须是有序序列
示例:
#include<iostream>
#include<vector>
#include<numeric>
#include<algorithm>
using namespace std;
void myPrint(int val)
{
cout << val << " ";
}
void test01()
{
vector<int> v1;
vector<int> v2;
for (int i = 0; i < 10; i++)
{
v1.push_back(i);//0~9
v2.push_back(i+5);//5~14
}
vector<int> vTarget;
//目标容器需要提前开辟空间
//最特殊的情况 大容器包含小容器
//开辟空间 取小容器的size即可
vTarget.resize(min(v1.size(), v2.size()));
vector<int>::iterator itEnd = set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());
for_each(vTarget.begin(), itEnd , myPrint);
cout << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
总结:
set_union(iterator beg1, iterator end1,iterator beg2, iterator end2,?iterator dest)
注意:两个集合必须是有序序列
//beg1容器1开始迭代器
//end1容器1结束迭代器
//beg2容器2开始迭代器
//end2容器2结束迭代器
//dest目标容器起始迭代器
#include<iostream>
#include<vector>
#include<numeric>
#include<algorithm>
using namespace std;
class myPrint
{
public:
void operator()(int val)
{
cout << val << " ";
}
};
//常用集合算法 set_union
void test01()
{
vector<int> v1;
vector<int> v2;
for (int i = 0; i < 10; i++)
{
v1.push_back(i);
v2.push_back(i + 5);
}
vector<int>vTarget;
//目标容器要提前开辟空间
//最特殊的情况 两个容器没有交集,并集就是两个容器size相加
vTarget.resize(v1.size() + v2.size());
vector<int>::iterator itEnd = set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());
for_each(vTarget.begin(), itEnd , myPrint());
cout << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
set_difference(iterator beg1, iterator end1,iterator beg2, iterator end2, terator dest)
注意:两个集合必须是有序序列
//beg1容器1开始迭代器
//end1容器1结束迭代器
//beg2容器2开始迭代器
//end2容器2结束迭代器
//dest目标容器起始迭代器
#include<iostream>
#include<vector>
#include<numeric>
#include <algorithm>
using namespace std;
class myPrint
{
public:
void operator()(int val)
{
cout << val << " ";
}
};
//常用集合算法 set_difference
void test01()
{
vector<int>v1;
vector<int>v2;
for (int i = 0; i < 10; i++)
{
v1.push_back(i);
v2.push_back(i + 5);
}
//创建目标容器
vector<int>vTarget;
//给目标容器开辟空间
//最特殊情况 两个容器没有交集,取两个容器中大的size作为目标容器开辟空间
vTarget.resize(max(v1.size(), v2.size()));
cout << "v1与v2的差集为:" << endl;
vector<int>::iterator itEnd = set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());
for_each(vTarget.begin(), itEnd, myPrint());
cout << endl;
cout << "———————————————————————————" << endl;
cout << "v2与v1的差集为:" << endl;
vector<int>::iterator itEnd1 = set_difference(v2.begin(), v2.end(), v1.begin(), v1.end(), vTarget.begin());
for_each(vTarget.begin(), itEnd1, myPrint());
cout << endl;
}
int main()
{
test01();
system("pause");
return 0;
}