概述:
5.1常用遍历算法
5.1.1for_each(遍历)
代码
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
//普通函数
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;
}
int main()
{
test01();
system("pause");
return 0;
}
总结:
for_each在实际开发中是最常用遍历算法,要熟练掌握
5.1.2 transform(搬运容器)
代码
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
class Transform
{
public:
int operator()(int val)
{
return val+100;
}
};
class print
{
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(), print());
cout << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
5.2常用查找算法
5.2.1 find(查指定元素)
代码
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<string>
//查找 内置数据
void test01()
{
vector<int>v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
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_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;
}
}
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 << "未找到!" << endl;
}
else
{
cout << "找到 姓名:" << it->m_Name << " 年龄:" << it->m_Age << endl;
}
}
int main()
{
//test01();
test02();
system("pause");
return 0;
}
5.2.2 find_if(按条件查找)
代码
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<string>
class GreaterFive
{
public:
bool operator()(int val)
{
return val > 5;
}
};
//查找1 内置数据
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(), GreaterFive());
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;
}
string m_Name;
int m_Age;
};
class Greater20
{
public:
bool operator() (const Person&p)
{
return p.m_Age > 20;
}
};
//查找2 自定义数据
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 << "未找到!" << endl;
}
else
{
cout << "找到 姓名:" << it->m_Name << " 年龄:" << it->m_Age << endl;
}
}
int main()
{
//test01();
test02();
system("pause");
return 0;
}
5.2.3 adjacent_find(查找相邻重复元素)
代码
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
void test01()
{
vector<int>v;
v.push_back(1);
v.push_back(1);
v.push_back(3);
v.push_back(1);
v.push_back(4);
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;
}
总结:面试题中如果出现查找相邻重复元素,记得使用stl的adjacent_find算法
5.2.4 binary_search(查找元素是否存在)
代码
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
void test01()
{
vector<int>v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
//注意:必须是有序序列
bool ret = binary_search(v.begin(), v.end(), 9);
if (ret)
{
cout << "找到该元素" << endl;
}
else
{
cout << "未找到!" << endl;
}
}
int main()
{
test01();
system("pause");
return 0;
}
总结:
虽然二分查找法效率很高,但使用时容器中的序列必须是有序序列
5.2.5 count(统计元素个数)
代码
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<string>
//统计 内部元素
void test01()
{
vector<int>v;
v.push_back(10);
v.push_back(30);
v.push_back(10);
v.push_back(20);
v.push_back(10);
v.push_back(50);
int sum = count(v.begin(), v.end(), 10);
cout << "10的个数为:" << sum << 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", 10);
Person p2("bbb", 20);
Person p3("ccc", 10);
Person p4("ddd", 40);
Person p5("eee", 10);
Person p6("fff", 20);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
v.push_back(p5);
v.push_back(p6);
Person p("ggg", 20);
int num = count(v.begin(), v.end(), p);
cout << "和ggg同岁的人有:" << num << endl;
}
int main()
{
//test01();
test02();
system("pause");
return 0;
}
总结:
统计自定义数据类型时,需要配合重载operator==
5.2.6 count_if
代码
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
class Greater20
{
public:
bool operator()(int val)
{
return val > 20;
}
};
//统计 内置数据
void test01()
{
vector<int>v;
v.push_back(10);
v.push_back(20);
v.push_back(50);
v.push_back(50);
v.push_back(20);
int num=count_if(v.begin(), v.end(), Greater20());
cout << "大于20的数据为:" << 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 AgeGreater20
{
public:
bool operator()(const Person&p)
{
return p.m_Age > 20;
}
};
void test02()
{
vector<Person>v;
Person p1("aaa", 10);
Person p2("bbb", 40);
Person p3("ccc", 20);
Person p4("ddd", 10);
Person p5("eee", 20);
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(), AgeGreater20());
cout << "年龄大于20的个数为:" << num << endl;
}
int main()
{
//test01();
test02();
system("pause");
return 0;
}
5.3常用排序算法
5.3.1 sort
代码
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
void print01(int val)
{
cout << val << " ";
}
void test01()
{
vector<int>v;
v.push_back(10);
v.push_back(50);
v.push_back(20);
v.push_back(40);
v.push_back(30);
//升序
sort(v.begin(), v.end());
for_each(v.begin(), v.end(), print01);
cout << endl;
//降序
sort(v.begin(), v.end(), greater<int>());
for_each(v.begin(), v.end(), print01);
cout << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
5.3.2 random_shuffle
代码
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<ctime>
void print01(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(), print01);
cout << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
5.3.3 merge
代码
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
void print(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(), print);
cout << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
总结:
merge合并的两个容器必须是有序序列
5.3.4 reverse
代码
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
void print(int val)
{
cout << val << " ";
}
void test01()
{
vector<int>v;
v.push_back(10);
v.push_back(50);
v.push_back(20);
v.push_back(40);
v.push_back(30);
cout << "反转前:" << endl;
for_each(v.begin(), v.end(), print);
cout << endl;
cout << "反转后:" << endl;
reverse(v.begin(), v.end());
for_each(v.begin(), v.end(), print);
cout << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
5.4常用拷贝和替换算法
5.4.1 copy
代码
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
void print(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(), print);
cout << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
5.4.2 replace
代码
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
class print
{
public:
void operator()(int val)
{
cout << val << " ";
}
};
void test01()
{
vector<int>v1;
v1.push_back(10);
v1.push_back(20);
v1.push_back(20);
v1.push_back(30);
cout << "替换前:" << endl;
for_each(v1.begin(), v1.end(), print());
cout << endl;
replace(v1.begin(), v1.end(), 20, 200);
cout << "替换后:" << endl;
for_each(v1.begin(), v1.end(), print());
cout << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
5.4.3 replace_if
代码
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
class print
{
public:
void operator()(int val)
{
cout << val << " ";
}
};
class Greater20
{
public:
bool operator()(int val)
{
return val >= 20;
}
};
void test01()
{
vector<int>v1;
v1.push_back(10);
v1.push_back(20);
v1.push_back(30);
v1.push_back(20);
v1.push_back(10);
cout << "替换前;" << endl;
for_each(v1.begin(), v1.end(), print());
cout << endl;
replace_if(v1.begin(), v1.end(), Greater20(), 100);
cout << "替换后;" << endl;
for_each(v1.begin(), v1.end(), print());
cout << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
5.4.4 swap
代码
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
void print(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 + 100);
}
cout << "交换前:" << endl;
for_each(v1.begin(), v1.end(), print);
cout << endl;
for_each(v2.begin(), v2.end(), print);
cout << endl;
cout << "————————" << endl;
cout << "交换后:" << endl;
swap(v1, v2);
for_each(v1.begin(), v1.end(), print);
cout << endl;
for_each(v2.begin(), v2.end(), print);
cout << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
5.5 常用算术生成算法
5.5.1 accumulate
代码
#include<iostream>
using namespace std;
#include<vector>
#include<numeric>
void test01()
{
vector<int>v;
for (int i = 0; i <=100; i++)
{
v.push_back(i);
}
//累加:参数3 起始累加值
int total=accumulate(v.begin(), v.end(), 0);
cout << "total= " << total << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
总结:
accumulate使用时头文件注意是numeric,这个算法很实用
5.5.2 fill
代码
#include<iostream>
using namespace std;
#include<vector>
#include<numeric>
#include<algorithm>
void print(int val)
{
cout << val << " ";
}
void test01()
{
vector<int>v;
v.resize(10);
//后期重新填充
fill(v.begin(), v.end(), 100);
for_each(v.begin(), v.end(), print);
cout << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
5.6常用集合算法
5.6.1set_intersection(交集)
代码
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
void print(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(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, print);
cout << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
总结:
1、求交集的两个集合必须是有序序列
2、目标容器开辟空间需要从两个容器取小值
3、set_intersection返回值既是交集中最后一个元素的位置
5.6.2set_union(并集)
代码
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
void print(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, print);
cout << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
5.6.3set_difference
代码
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
void print(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()));
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, print);
cout << endl;
cout << "v2和v1的差集:" << endl;
vector<int>::iterator itend = set_difference(v2.begin(), v2.end(), v1.begin(), v1.end(), vTarget.begin());
for_each(vTarget.begin(), itend, print);
cout << endl;
}
int main()
{
test01();
system("pause");
return 0;
}