常用遍历算法
for_each
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
using namespace std;
//常用遍历算法 - for_each
//普通函数
void print01(int val)
{
cout << val << " ";
}
//仿函数
class print02 {
public:
void operator()(int v)
{
cout << v << " ";
}
};
void test01()
{
vector<int> v;
for (int i = 0; i < 10; i++) {
v.push_back(i);
}
// for_each(v.begin(), v.end(), print01);
for_each(v.begin(), v.end(), print02());
cout<<endl;
}
int main()
{
test01();
}
总结:for_each在实际开发中是最常用的遍历算法,需要熟练掌握。
transform
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
using namespace std;
//常用算法 - transform
//仿函数
class Transform {
public:
int operator()(int v)
{
return v + 10;
}
};
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> v1;
v1.resize(v.size()); // resize指定大小,不是reverse颠倒
transform(v.begin(), v.end(), v1.begin(), Transform()); //第四个函数是仿函数
for_each(v1.begin(), v1.end(), MyPrint());
cout << endl;
}
int main()
{
test01();
}
常用查找算法
find算法
功能描述:
查找指定元素,找到返回指定元素的迭代器,找不到返回结束迭代器end()
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
using namespace std;
//常用查找算法 - find
//查找内置数据类型
void test01()
{
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
v.push_back(5);
vector<int>::iterator pos = find(v.begin(), v.end(), 21);
if (pos == v.end()) {
cout << "没有找到" << endl;
} else {
cout << "找到了" << *pos << endl;
}
}
class Person {
public:
string m_Name;
int m_Age;
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;
}
}
};
//查找自定义数据类型
void test02()
{
vector<Person> v;
Person p1("aa", 10);
Person p2("bb", 20);
Person p3("cc", 30);
Person p4("dd", 40);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
Person pp("ces", 12);
//自定义的数据类型需要重载==号
vector<Person>::iterator pos = find(v.begin(), v.end(), p1);
if (pos == v.end()) {
cout << "没找到" << endl;
} else {
cout << "找到了" << pos->m_Name << " " << pos->m_Age << endl;
}
}
int main()
{
test02();
}
总结:利用find可以在容器中找指定的元素,返回值是迭代器
find_if
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
using namespace std;
//常用查找算法 - find_if
//查找内置数据类型
class GreaterFive {
public:
bool operator()(int v)
{
return v > 3;
}
};
void test01()
{
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
v.push_back(5);
vector<int>::iterator pos = find_if(v.begin(), v.end(), GreaterFive());
if (pos == v.end()) {
cout << "没找到" << endl;
} else {
cout << "找到了 :" << *pos << endl;
}
}
//查找自定义数据类型
class Person {
public:
string m_Name;
int m_Age;
Person(string name, int age)
{
this->m_Name = name;
this->m_Age = age;
}
};
class Greater20 {
public:
bool operator()(Person& p)
{
if (p.m_Age > 20) {
return true;
} else {
return false;
}
}
};
void test02()
{
vector<Person> v;
Person p1("aa", 10);
Person p2("bb", 20);
Person p3("cc", 30);
Person p4("dd", 40);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
Person pp("ces", 12);
vector<Person>::iterator pos = find_if(v.begin(), v.end(), Greater20());
while (true) {
if (pos == v.end()) {
cout << "没找到" << endl;
break;
} else {
cout << "找到了" << pos->m_Name << " " << pos->m_Age << endl;
pos++;
}
}
}
int main()
{
test02();
}
adjacent_find
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
using namespace std;
//常用查找算法 - adjacent_find
void test01()
{
vector<int> v;
v.push_back(1);
v.push_back(3); //返回这里的位置
v.push_back(3);
v.push_back(4);
v.push_back(2);
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();
}
binary_search
功能描述:
查找指定元素是否存在
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
using namespace std;
//常用查找算法 - binary_search 二分查找
void test01()
{
vector<int> v;
for (int i = 0; i < 10; i++) {
v.push_back(i);
}
// v.push_back(2);
//查找容器中是否有元素9
// 二分查找必须是排序好的序列,无序序列不可用
bool exists = binary_search(v.begin(), v.end(), 9);
if (exists) {
cout << "存在" << endl;
} else {
cout << "不存在" << endl;
}
}
int main()
{
test01();
}
总结:二分查找法查找的效率很高,值得注意的是查找的容器中元素必须有序序列
count
功能描述:
统计元素个数
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
using namespace std;
//常用查找算法 - count
// 1. 统计内置数据类型
void test01()
{
vector<int> v;
v.push_back(10);
v.push_back(20);
v.push_back(10);
v.push_back(30);
v.push_back(30);
v.push_back(40);
int number = count(v.begin(), v.end(), 40);
cout << "40的元素个数:" << number << endl;
}
// 2.统计自定义数据类型
class Person {
public:
string m_name;
int m_age;
Person(string name, int age)
{
this->m_name = name;
this->m_age = age;
}
bool operator==(const Person& p)
{
return p.m_age == this->m_age && p.m_name == this->m_name;
}
};
void test02()
{
Person p1("张三", 12);
Person p2("李四", 14);
Person p3("张三", 12);
Person p4("王五", 11);
Person p5("赵六", 12);
vector<Person> v;
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
v.push_back(p5);
int ret = count(v.begin(), v.end(), p3);
cout << "p3出现的个数" << ret << endl;
}
int main()
{
test02();
}
总结:统计自定义数据类型时候,需要配合operator==
cout_if
功能描述:
按条件统计元素个数
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
using namespace std;
//常用查找算法 - count_if
// 1. 统计内置数据类型
class Mycount {
public:
bool operator()(int val)
{
return val > 10;
}
};
void test01()
{
vector<int> v;
v.push_back(10);
v.push_back(20);
v.push_back(10);
v.push_back(30);
v.push_back(30);
v.push_back(40);
int number = count_if(v.begin(), v.end(), Mycount());
cout << "大于10的元素个数有" << number << endl;
}
// 2.统计自定义数据类型
class Person {
public:
string m_name;
int m_age;
Person(string name, int age)
{
this->m_name = name;
this->m_age = age;
}
bool operator==(const Person& p)
{
return p.m_age == this->m_age && p.m_name == this->m_name;
}
};
class Mycount20 {
public:
bool operator()(Person& p)
{
return p.m_age > 12;
}
};
void test02()
{
Person p1("张三", 12);
Person p2("李四", 14);
Person p3("张三", 12);
Person p4("王五", 11);
Person p5("赵六", 12);
vector<Person> v;
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
v.push_back(p5);
int ret = count_if(v.begin(), v.end(), Mycount20());
cout << "年龄在11以上的人数" << ret << endl;
}
int main()
{
// test01();
test02();
}
排序算法
学习目标:
掌握常用的排序算法
sort排序
功能描述:
对容器内元素进行排序
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
using namespace std;
//常用排序算法 sort
void printV(int val)
{
cout << val << " ";
}
void test02()
{
vector<int> v;
v.push_back(10);
v.push_back(30);
v.push_back(0);
v.push_back(20);
v.push_back(50);
sort(v.begin(), v.end());
for_each(v.begin(), v.end(), printV);
cout << endl;
for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
cout << *it << " ";
}
cout << endl;
//改为降序
sort(v.begin(), v.end(), greater<int>()); // greater变为降序
for_each(v.begin(), v.end(), printV);
cout <<endl;
}
int main()
{
// test01();
test02();
}
总结:sort属于开发中最常用的算法之一,要熟练掌握
random_shuffle
功能描述:
洗牌 指定范围内的元素随机调整次序
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
using namespace std;
//常用排序算法 sort
void printV(int val)
{
cout << val << " ";
}
void test02()
{
vector<int> v;
v.push_back(10);
v.push_back(30);
v.push_back(0);
v.push_back(20);
v.push_back(50);
for_each(v.begin(), v.end(), printV);
cout << endl;
//利用洗牌算法,打乱牌的顺序
random_shuffle(v.begin(), v.end());
for_each(v.begin(), v.end(), printV);
cout << endl;
}
int main()
{
srand((unsigned int)time(NULL));
test02();
}
merge
功能描述:
两个容器元素合并,并存储到另一个容器中
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
using namespace std;
//常用排序算法 merge
void printV(int val)
{
cout << val << " ";
}
void test02()
{
vector<int> v1;
vector<int> v2;
for (int i = 0; i < 5; i++) {
v1.push_back(i);
v2.push_back(i + 1);
}
for_each(v1.begin(), v1.end(), printV);
cout << endl;
for_each(v2.begin(), v2.end(), printV);
cout << endl;
//目标容器
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(), printV);
cout<<endl;
}
int main()
{
test02();
}
总结:merge容器需要提前开辟空间,合并的时候必须保证两个容器都是有序序列
reverse
功能描述:
将容器内的元素进行反转
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
using namespace std;
//常用排序算法 reverse
void printV(int val)
{
cout << val << " ";
}
void test02()
{
vector<int> v1;
for (int i = 0; i < 5; i++) {
v1.push_back(i);
}
for_each(v1.begin(), v1.end(), printV);
cout << endl;
cout << "反转后" << endl;
reverse(v1.begin(), v1.end());
for_each(v1.begin(), v1.end(), printV);
cout << endl;
}
int main()
{
test02();
}
常用的拷贝和替换算法
学习目标:
掌握常用的拷贝和替换算法
copy
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
using namespace std;
//常用拷贝和替换算法 copy
void printV(int val)
{
cout << val << " ";
}
void test02()
{
vector<int> v1;
v1.push_back(10);
v1.push_back(20);
for_each(v1.begin(), v1.end(), printV);
cout << endl;
vector<int> v2;
v2.resize(v1.size());
copy(v1.begin(), v1.end(), v2.begin());
for_each(v2.begin(), v2.end(), printV);
cout << endl;
}
int main()
{
test02();
}
replace
功能描述:
将容器内指定范围的旧元素修改为新元素
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
using namespace std;
//常用拷贝和替换算法 replace
void printV(int val)
{
cout << val << " ";
}
void test02()
{
vector<int> v1;
v1.push_back(10);
v1.push_back(20);
v1.push_back(10);
v1.push_back(30);
for_each(v1.begin(), v1.end(), printV);
cout << endl;
vector<int> v2;
cout << "替换后" << endl;
replace(v1.begin(), v1.end(), 10, 100);
for_each(v1.begin(), v1.end(), printV);
cout << endl;
}
int main()
{
test02();
}
总结:replace会替换区间内满足条件的元素
replace_if
功能描述:
将区间内的满足条件的元素,替换成指定元素
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
using namespace std;
//常用拷贝和替换算法 replace_if
void printV(int val)
{
cout << val << " ";
}
class Myreplace {
public:
bool operator()(int val)
{
return val > 10;
}
};
void test02()
{
vector<int> v1;
v1.push_back(10);
v1.push_back(20);
v1.push_back(10);
v1.push_back(30);
for_each(v1.begin(), v1.end(), printV);
cout << endl;
vector<int> v2;
cout << "替换后" << endl;
replace_if(v1.begin(), v1.end(), Myreplace(), 100);
for_each(v1.begin(), v1.end(), printV);
cout << endl;
}
int main()
{
test02();
}
总结:按条件替换,可以利用仿函数灵活筛选满足的条件
swap
功能描述:
互换两个容器的元素
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
using namespace std;
//常用拷贝和替换算法 replace_if
void printV(int val)
{
cout << val << " ";
}
class Myreplace {
public:
bool operator()(int val)
{
return val > 10;
}
};
void test02()
{
vector<int> v1;
v1.push_back(10);
v1.push_back(20);
v1.push_back(10);
v1.push_back(30);
vector<int> v2;
v2.push_back(100);
swap(v1, v2);
for_each(v1.begin(), v1.end(), printV);
cout << endl;
for_each(v2.begin(), v2.end(), printV);
cout << endl;
}
int main()
{
test02();
}
常用的算数生成算法
accumlate
#include <algorithm>
#include <ctime>
#include <iostream>
#include <numeric>
#include <string>
using namespace std;
void test01()
{
vector<int> v;
for (int i = 0; i < 10; i++) {
v.push_back(i);
}
int total = accumulate(v.begin(), v.end(), 0);//从0开始进行累加
cout << "总和" << total << endl;
}
int main()
{
test01();
return 0;
}
fill
#include <algorithm>
#include <ctime>
#include <iostream>
#include <numeric>
#include <string>
//常用算术生成算法 fill
using namespace std;
void printV(int val)
{
cout << val << " ";
}
void test01()
{
vector<int> v;
v.resize(10, 1); //设置大小,默认指定为0
for_each(v.begin(), v.end(), printV);
cout << endl;
fill(v.begin(), v.end(), 100);
for_each(v.begin(), v.end(), printV);
cout << endl;
}
int main()
{
test01();
return 0;
}
常用集合算法
set_intersection
#include <algorithm>
#include <ctime>
#include <iostream>
#include <numeric>
#include <string>
//常用集合算法 set_intersection 交集
using namespace std;
void printV(int val)
{
cout << val << " ";
}
void test01()
{
vector<int> v;
vector<int> v1;
vector<int> vTarget;
for (int i = 0; i < 10; i++) {
v.push_back(i); // 0-9
v1.push_back(i + 5); // 5-14
}
// v.push_back(10);
cout << "v1" << endl;
for_each(v1.begin(), v1.end(), printV);
cout << endl;
cout << "v" << endl;
for_each(v.begin(), v.end(), printV);
cout << endl;
//提前开辟空间
//最特殊的情况是两个取小的 开辟空间,取小容器的size
vTarget.resize(min(v1.size(), v.size()));
vector<int>::iterator it = set_intersection(v1.begin(), v1.end(), v.begin(), v.end(), vTarget.begin()); //返回迭代器
for_each(vTarget.begin(), it, printV); //需要使用别人的迭代器
cout << endl;
}
int main()
{
test01();
return 0;
}
set_union
功能描述:
求两个集合的并集
#include <algorithm>
#include <ctime>
#include <iostream>
#include <numeric>
#include <string>
//常用集合算法 set_union 并集
using namespace std;
void printV(int val)
{
cout << val << " ";
}
void test01()
{
vector<int> v;
vector<int> v1;
vector<int> vTarget;
for (int i = 0; i < 10; i++) {
v.push_back(i); // 0-9
v1.push_back(i + 5); // 5-14
}
vTarget.resize((v.size() + v1.size()));
vector<int>::iterator it = set_union(v.begin(), v.end(), v1.begin(), v1.end(), vTarget.begin());
for_each(vTarget.begin(), it, printV);
cout << endl;
}
int main()
{
test01();
return 0;
}
总结:
set_difference
功能描述:
求两个集合的差集
#include <algorithm>
#include <ctime>
#include <iostream>
#include <numeric>
#include <string>
//常用集合算法 set_difference
using namespace std;
void printV(int val)
{
cout << val << " ";
}
void test01()
{
vector<int> v;
vector<int> v1;
vector<int> vTarget;
vector<int> vTarget2;
for (int i = 0; i < 10; i++) {
v.push_back(i); // 0-9
v1.push_back(i + 5); // 5-14
}
vTarget.resize(max(v1.size(), v.size()));
vector<int>::iterator it = set_difference(v.begin(), v.end(), v1.begin(), v1.end(), vTarget.begin());
for_each(vTarget.begin(), it, printV);
cout << endl;
vTarget2.reserve(max(v1.size(), v.size())); //特殊情况取最大的容器空间
vector<int>::iterator it1 = set_difference(v1.begin(), v1.end(), v.begin(), v.end(), vTarget.begin());
for_each(vTarget.begin(), it1, printV);
cout << endl;
}
int main()
{
test01();
return 0;
}