目录
5.1、遍历算法
5.1.1、for_each
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
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());
}
int main()
{
test01();
system("pause");
}
5.1.2、transfrom
搬运目标容器前需要提前开辟空间
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
class Transform
{
public:
int operator()(int v )
{
return v;
}
};
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());
}
int main()
{
test01();
system("pause");
}
5.2、查找算法
5.2.1、find
find可以在容器中找指定的元素,返回值是迭代器
#include <iostream>
#include <vector>
#include <string>
#include<algorithm>
using namespace std;
//查找内置数据类型
void test01()
{
vector<int>v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
//查找容器中是否有5
vector<int>::iterator it = find(v.begin(), v.end(), 5);
if (it == v.end())
{
cout << "未找到"<<endl;
}
else
{
cout << "找到了元素:"<<*it<<endl;
}
}
class Person
{
public:
Person(string name, int age)
{
this->m_Age = age;
this->m_Name = name;
}
//重载 == 让底层的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;
}
}
int m_Age;
string m_Name;
};
//查找自定义的数据类型
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);
Person p9("bbb", 20);
vector<Person>::iterator it = find(v.begin(), v.end(), p9);
if (it == v.end())
{
cout << "未找到" << endl;
}
else
{
cout << "找到了元素,姓名:" << it->m_Name <<" 年龄:"<<it->m_Age << endl;
}
}
int main()
{
test02();
test01();
system("pause");
}
5.2.2、find_if
#include <iostream>
#include <vector>
#include <string>
#include<algorithm>
using namespace std;
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);
}
//查找容器中是否有5
vector<int>::iterator it = find_if(v.begin(), v.end(),GreatFive());
if (it == v.end())
{
cout << "未找到"<<endl;
}
else
{
cout << "找到了大于5的元素:"<<*it<<endl;
}
}
class Person
{
public:
Person(string name, int age)
{
this->m_Age = age;
this->m_Name = name;
}
//重载 == 让底层的find知道如何对比person数据类型
int m_Age;
string m_Name;
};
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);
vector<Person>::iterator it = find_if(v.begin(), v.end(), Great20());
if (it == v.end())
{
cout << "未找到" << endl;
}
else
{
cout << "找到了元素,姓名:" << it->m_Name <<" 年龄:"<<it->m_Age << endl;
}
}
int main()
{
test02();
test01();
system("pause");
}
5.2.3、adjacent_find
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
void test01()
{
vector<int>v;
v.push_back(0);
v.push_back(2);
v.push_back(0);
v.push_back(3);
v.push_back(1);
v.push_back(4);
v.push_back(3);
v.push_back(3);
vector<int>::iterator it = adjacent_find(v.begin(), v.end());
if(it == v.end())
{
cout << "未找到" << endl;
}
else
{
cout << "找到了相邻重复元素元素:" <<*it << endl;
}
}
int main()
{
test01();
system("pause");
}
5.2.4、binary_search
虽然二分查找法效率很高,但是查找的容器必须是有序的序列
#include <iostream>
#include <string>
using namespace std;
#include<vector>
#include<algorithm>
//查找算法
void test01()
{
vector<int>v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
//查找容器中是否有9
bool ret = binary_search(v.begin(), v.end(), 9);
if (ret)
{
cout << "已找到"<<endl;
}
else
{
cout << "未找到"<<endl;
}
}
int main()
{
test01();
system("pause");
}
5.2.5、count
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
//1、查找内置数据类型
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;
}
//2、查找自定义数据类型
class Person
{
public:
Person(string name, int age)
{
this->m_Age = age;
this->m_Name = name;
}
bool operator==(const Person &p)
{
if (this->m_Age == p.m_Age)
{
return true;
}
else
{
return false;
}
}
int m_Age;
string m_Name;
};
void test02()
{
vector<Person>v;
Person p1("刘备", 35);
Person p2("关羽", 35);
Person p3("张飞", 30);
Person p4("赵云", 35);
Person p5("曹操", 40);
Person p("诸葛亮", 35);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
v.push_back(p5);
int num = count(v.begin(), v.end(), p);
cout << "和诸葛亮同岁的人有" << num << "个" << endl;
}
int main()
{
test02();
test01();
system("pause");
}
5.2.6、count_if
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
class Great02
{
public:
bool operator()(int val)
{
return val > 20;
}
};
//1、查找内置数据类型
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_if(v.begin(), v.end(), Great02());
cout << "大于20的元素个数为:" << num << endl;
}
//2、查找自定义数据类型
class Person
{
public:
Person(string name, int age)
{
this->m_Age = age;
this->m_Name = name;
}
int m_Age;
string m_Name;
};
class AgeGreat20
{
public:
bool operator()(const Person &p)
{
if (p.m_Age>20)
{
return true;
}
else
{
return false;
}
}
};
void test02()
{
vector<Person>v;
Person p1("刘备", 35);
Person p2("关羽", 35);
Person p3("张飞", 30);
Person p4("赵云", 35);
Person p5("曹操", 40);
Person p("诸葛亮", 35);
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()
{
test02();
test01();
system("pause");
}
5.3、排序算法
5.3.1、sort
#include <iostream>
#include <string>
#include<algorithm>
#include <vector>
using namespace std;
void myPrint(int val)
{
cout << val << " ";
}
void test01()
{
vector<int>v;
v.push_back(10);
v.push_back(50);
v.push_back(30);
v.push_back(20);
v.push_back(40);
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;
}
int main()
{
test01();
system("pause");
}
5.3.2、random_shuffle
random_shuffle比较实用,使用洗牌算法时要加上随机数的种子
#include <iostream>
#include <string>
#include<algorithm>
#include <vector>
#include <ctime>
using namespace std;
void myPrint(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);
}
random_shuffle(v.begin(), v.end());
for_each(v.begin(), v.end(), myPrint);
cout << endl;
}
int main()
{
test01();
system("pause");
}
5.3.3、merge
将两个有序序列合在一起,形成一个新的有序序列,两个序列的排序方法必须一致
#include <iostream>
#include <string>
#include<algorithm>
#include <vector>
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);
v2.push_back(i+1);
}
vector<int>vTarget;
vTarget.resize(v1.size() + v2.size());
merge(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());
for_each(vTarget.begin(), vTarget.end(), myPrint);
cout << endl;
}
int main()
{
test01();
system("pause");
}
5.3.4、reserve
#include <iostream>
#include <string>
#include<algorithm>
#include <vector>
using namespace std;
void myPrint(int val)
{
cout << val << " ";
}
void test01()
{
vector<int>v;
v.push_back(10);
v.push_back(20);
v.push_back(50);
v.push_back(30);
v.push_back(40);
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");
}
5.4、拷贝和替换算法
5.4.1、copy
利用copy拷贝函数的时候,注意要提前开辟空间,避免出现空间不足的情况。
#include <iostream>
#include <string>
#include<algorithm>
#include <vector>
using namespace std;
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());
for_each(v2.begin(), v2.end(), myPrint);
}
int main()
{
test01();
system("pause");
}
5.4.2、replace
#include <iostream>
#include <string>
#include<algorithm>
#include <vector>
using namespace std;
class MyPrint
{
public:
void operator()(int val)
{
cout << val << " ";
}
};
void test01()
{
vector<int>v;
v.push_back(10);
v.push_back(30);
v.push_back(40);
v.push_back(20);
v.push_back(10);
v.push_back(30);
v.push_back(50);
v.push_back(20);
v.push_back(50);
v.push_back(30);
v.push_back(40);
cout << "替换前" << endl;
for_each(v.begin(), v.end(), MyPrint());
cout << endl;
replace(v.begin(), v.end(), 20, 2000);
cout << "替换后" << endl;
for_each(v.begin(), v.end(), MyPrint());
cout << endl;
}
int main()
{
test01();
system("pause");
}
5.4.3、replace_if
#include <iostream>
#include <string>
#include<algorithm>
#include <vector>
using namespace std;
class MyPrint
{
public:
void operator()(int val)
{
cout << val << " ";
}
};
class Greater30
{
public:
bool operator()(int val)
{
return val > 30;
}
};
void test01()
{
vector<int>v;
v.push_back(10);
v.push_back(30);
v.push_back(40);
v.push_back(20);
v.push_back(10);
v.push_back(30);
v.push_back(50);
v.push_back(20);
v.push_back(50);
v.push_back(30);
v.push_back(40);
cout << "替换前" << endl;
for_each(v.begin(), v.end(), MyPrint());
cout << endl;
replace_if(v.begin(), v.end(), Greater30(),3000);
cout << "替换后" << endl;
for_each(v.begin(), v.end(), MyPrint());
cout << endl;
}
int main()
{
test01();
system("pause");
}
5.4.4、swap
交换容器的时候要同种类型
#include <iostream>
#include <string>
#include<algorithm>
#include <vector>
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);
v2.push_back(i+1000);
}
cout << "交换前" << endl;
for_each(v1.begin(), v1.end(), myPrint);
cout << endl;
for_each(v2.begin(), v2.end(), myPrint);
cout << endl;
swap(v1, v2);
cout << "交换后" << endl;
for_each(v1.begin(), v1.end(), myPrint);
cout << endl;
for_each(v2.begin(), v2.end(), myPrint);
cout << endl;
}
int main()
{
test01();
system("pause");
}
5.5、算术生成算法
5.5.1、accumulate
accumulate使用时的头文件是numeric,这个算法很实用
#include <iostream>
#include <string>
#include<algorithm>
#include <vector>
#include <numeric>
using namespace std;
void test01()
{
vector<int>v;
for (int i = 0; i < 101; i++)
{
v.push_back(i);
}
int total = accumulate(v.begin(), v.end(), 0);
cout << "total = " << total << endl;
}
int main()
{
test01();
system("pause");
}
5.5.2、fill
利用fill可以将容器区间的值填充为指定的值
#include <iostream>
#include <string>
#include<algorithm>
#include <vector>
using namespace std;
void myPrint(int val)
{
cout << val << " ";
}
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");
}
5.6、集合算法
5.6.1set_intersection
原容器必须是有序的序列
目标容器需要提前开辟空间
最特殊的情况 大容器包含小容器 开辟空间 取小容器的size即可
返回值是最后一个值的位置
#include <iostream>
#include <string>
#include<algorithm>
#include <vector>
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);
v2.push_back(i + 5);
}
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");
}
5.6.2、set_union
求并集首先是有序序列
特殊情况是两个容器相加
ste_union返回值既是并集中最后一个元素的位置
#include <iostream>
#include <string>
#include<algorithm>
#include <vector>
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);
v2.push_back(i + 5);
}
vector<int>vTarget;
//目标容器需要提前开辟空间
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");
}
5.6.3、set_difference
求并集首先是有序序列
特殊情况是两个容器中最大的
ste_difference返回值既是差集中最后一个元素的位置
#include <iostream>
#include <string>
#include<algorithm>
#include <vector>
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);
v2.push_back(i + 5);
}
vector<int>vTarget;
//目标容器需要提前开辟空间
vTarget.resize(max(v1.size(),v2.size()));
//获取交集
vector<int>::iterator itEnd = set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());
for_each(vTarget.begin(), itEnd, myPrint);
cout << endl;
}
int main()
{
test01();
system("pause");
}
完结撒花
❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀❀