概述:
一、常用遍历算法
1、for_each
函数原型:
for_each(iterator beg,iterator end,_func);
遍历算法,遍历容器元素
beg开始迭代器
end结束迭代器
_func函数或者函数对象
//普通函数
void print1(int val)
{
cout << val << " ";
}
//仿函数
class print2
{
public:
void operator()(int val)
{
cout << val << " ";
}
};
void test01()
{
vector v;
v.push_back(2);
v.push_back(6);
v.push_back(15);
v.push_back(8);
v.push_back(26);
v.push_back(56);
v.push_back(16);
v.push_back(33);
v.push_back(21);
for_each(v.begin(), v.end(), print1);//普通函数遍历
cout << endl;
for_each(v.begin(), v.end(), print2());//仿函数遍历
}
int main()
{
test01();
system("pause");
return 0;
}
2、transform
功能:搬运容器到另一个容器中
函数原型:
transform(iterator beg1,iterator end1,iterator beg2,_func);
beg1 源容器开始迭代器
end1 源容器结束迭代器
beg2 目标容器开始迭代器
_func 函数或函数对象
//搬运过程可以做一些逻辑运算
class transform1
{
public:
int operator()(int val)
{
return val;
}
};
void test01()
{
vector v;
v.push_back(2);
v.push_back(6);
v.push_back(15);
v.push_back(8);
v.push_back(26);
v.push_back(56);
v.push_back(16);
v.push_back(33);
v.push_back(21);
vector v1;
v1.resize(v.size());//搬运前给目标容器分配内存
transform(v.begin(), v.end(), v1.begin(), transform1());
for_each(v1.begin(), v1.end(), print1);
}
int main()
{
test01();
system("pause");
return 0;
}
二、常用查找算法
算法简介:
1、find
功能描述:查找指定元素,找到返回元素的迭代器,找不到返回结束迭代器end()
函数原型:
find(iterator beg,iterator end,value);
按值查找元素,找到返回指定位置迭代器,找不到返回结束迭代器位置
beg开始迭代器
end结束迭代器
value查找的元素
//查找内置数据类型
void test01()
{
vector v;
v.push_back(2);
v.push_back(6);
v.push_back(15);
v.push_back(8);
v.push_back(26);
v.push_back(56);
v.push_back(16);
v.push_back(33);
v.push_back(21);
vector::iterator it = find(v.begin(), v.end(), 6);
if (it == v.end())
{
cout << "没有找到" << endl;
}
else {
cout << "找到了" << *it << endl;
}
}
class person
{
public:
string m_name;
int m_age;
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;
}
}
};
void test02()
{
vector v1;
person p1("aaa", 20);
person p2("bbb", 30);
v1.push_back(p1);
v1.push_back(p2);
vector::iterator it = find(v1.begin(), v1.end(), p2);
if (it == v1.end())
{
cout << "no" << endl;
}
else {
cout << "yes " << it->m_name << " " << (*it).m_age << endl;
}
}
int main()
{
test01();
test02();
system("pause");
return 0;
}
2、find_if
函数原型:
find_if(iterator beg,iterator end,pred_);
按值查找元素,找到返回指定位置迭代器,找不到返回end迭代器
beg开始迭代器
end结束迭代器
pred_ 函数或者谓词
class greatfive
{
public:
int operator()(int val)
{
return val > 5;
}
};
//查找内置数据类型
void test01()
{
vector v;
v.push_back(2);
v.push_back(6);
v.push_back(15);
v.push_back(8);
v.push_back(26);
v.push_back(56);
v.push_back(16);
v.push_back(33);
v.push_back(21);
vector::iterator it = find_if(v.begin(), v.end(), greatfive());
if (it == v.end())
{
cout << "没有找到" << endl;
}
else {
cout << "找到了" << *it << endl;
}
}
class person
{
public:
string m_name;
int m_age;
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;
}
}
};
class great20
{
public:
bool operator()(person &p)
{
return p.m_age > 20;
}
};
void test02()
{
vector v1;
person p1("aaa", 20);
person p2("bbb", 30);
v1.push_back(p1);
v1.push_back(p2);
vector::iterator it = find_if(v1.begin(), v1.end(), great20());
if (it == v1.end())
{
cout << "no" << endl;
}
else {
cout << "yes " << it->m_name << " " << (*it).m_age << endl;
}
}
int main()
{
test01();
test02();
system("pause");
return 0;
}
3、adjacent_find
查找相邻重复元素
函数原型:
adjacent_find(iterator beg,iterator end)
查找相邻重复元素,返回相邻元素的第一个位置迭代器
beg开始迭代器
end结束迭代器
void test01()
{
vector v;
v.push_back(2);
v.push_back(6);
v.push_back(15);
v.push_back(15);
v.push_back(26);
v.push_back(56);
v.push_back(16);
v.push_back(16);
v.push_back(56);
vector::iterator it = adjacent_find(v.begin(), v.end());
if (it == v.end())
{
cout << "没有找到" << endl;
}
else {
cout << "找到了" << *it << endl;
}
}
int main()
{
test01();
//test02();
system("pause");
return 0;
}
4、binary_search
查找指定元素是否存在
!!!容器中的元素必须是有序的序列
函数原型:
binary_find(iterator beg,iterator end,value)
查找指定的元素,找到返回true 找不到返回false
beg开始迭代器
end结束迭代器
value 指定的查找元素
void test01()
{
set v;
v.insert(2);
v.insert(6);
v.insert(15);
v.insert(26);
v.insert(56);
v.insert(16);
bool it = binary_search(v.begin(), v.end(),10);
if (!it)
{
cout << "没有找到" << endl;
}
else {
cout << "找到了" << endl;
}
}
int main()
{
test01();
//test02();
system("pause");
return 0;
}
5、count
功能:统计元素个数
函数原型:
count(iterator beg,iterator end,value)
统计元素出现次数
beg开始迭代器
end结束迭代器
value 指定的统计元素
void test01()
{
vector v;
v.push_back(10);
v.push_back(20);
v.push_back(50);
v.push_back(14);
v.push_back(67);
v.push_back(153);
v.push_back(167);
v.push_back(14);
v.push_back(26);
v.push_back(14);
v.push_back(89);
v.push_back(75);
int num = count(v.begin(), v.end(),14);
cout << num << endl;
}
class person
{
public:
string m_name;
int m_age;
person(string name, int age)
{
this->m_name = name;
this->m_age = age;
}
//重载==,让底层find知道如何对比person数据类型
bool operator==(const person&p)
{
if (this->m_age == p.m_age)
{
return true;
}
else
{
return false;
}
}
};
void test02()
{
vector v1;
person p1("aaa", 20);
person p2("bbb", 30);
person p3("aaa", 20);
person p4("aaa", 20);
person p5("aaa", 60);
person p6("ccc", 50);
v1.push_back(p1);
v1.push_back(p2);
v1.push_back(p3);
v1.push_back(p4);
v1.push_back(p5);
v1.push_back(p6);
person p("cc", 20);
int num = count(v1.begin(), v1.end(), p);
cout << num << endl;
}
int main()
{
test01();
test02();
system("pause");
return 0;
}
6、count_if
按条件统计元素个数
函数原型:
count_if(iterator beg,iterator end,pred_)
按照条件统计元素出现次数
beg开始迭代器
end结束迭代器
pred_ 谓词
class greatfive
{
public:
int operator()(int val)
{
return val > 20;
}
};
//查找内置数据类型
void test01()
{
vector v;
v.push_back(10);
v.push_back(20);
v.push_back(50);
v.push_back(14);
v.push_back(67);
v.push_back(153);
v.push_back(167);
v.push_back(14);
v.push_back(26);
v.push_back(14);
v.push_back(89);
v.push_back(75);
int num = count_if(v.begin(), v.end(),greatfive());
cout << num << endl;
}
class person
{
public:
string m_name;
int m_age;
person(string name, int age)
{
this->m_name = name;
this->m_age = age;
}
//重载==,让底层find知道如何对比person数据类型
bool operator==(const person&p)
{
if (this->m_age == p.m_age)
{
return true;
}
else
{
return false;
}
}
};
class great20
{
public:
bool operator()(person &p)
{
return p.m_age > 20;
}
};
void test02()
{
vector v1;
person p1("aaa", 20);
person p2("bbb", 30);
person p3("aaa", 20);
person p4("aaa", 20);
person p5("aaa", 60);
person p6("ccc", 50);
v1.push_back(p1);
v1.push_back(p2);
v1.push_back(p3);
v1.push_back(p4);
v1.push_back(p5);
v1.push_back(p6);
int num = count_if(v1.begin(), v1.end(), great20());
cout << num << endl;
}
int main()
{
test01();
test02();
system("pause");
return 0;
}
7
3
请按任意键继续. . .
三、常用排序算法
算法简介:
1、sort
函数原型:
sort(iterator beg,iterator end,pred_)
按照条件对元素进行排序
beg开始迭代器
end结束迭代器
pred_ 谓词
class compare1
{
public:
int operator()(int val1,int val2)
{
return val1 > val2;
}
};
//查找内置数据类型
void test01()
{
vector v;
v.push_back(10);
v.push_back(20);
v.push_back(50);
v.push_back(14);
v.push_back(67);
v.push_back(153);
v.push_back(167);
v.push_back(14);
v.push_back(26);
v.push_back(14);
v.push_back(89);
v.push_back(75);
sort(v.begin(), v.end());
for_each(v.begin(), v.end(),print2());
cout << endl;
cout << "---------------------" << endl;
//降序排列
sort(v.begin(), v.end(),compare1());
for_each(v.begin(), v.end(), print2());
}
int main()
{
test01();
//test02();
system("pause");
return 0;
}
10 14 14 14 20 26 50 67 75 89 153 167
---------------------
167 153 89 75 67 50 26 20 14 14 14 10 请按任意键继续. . .
2、random_shuffle
功能:指定范围内元素随机调整次序
函数原型:
random_shuffle(iterator beg,iterator end)
指定范围内元素随机调整次序
beg开始迭代器
end结束迭代器
void test01()
{
vector v;
v.push_back(10);
v.push_back(20);
v.push_back(50);
v.push_back(14);
v.push_back(67);
v.push_back(153);
v.push_back(167);
v.push_back(14);
v.push_back(26);
v.push_back(14);
v.push_back(89);
v.push_back(75);
sort(v.begin(), v.end());
for_each(v.begin(), v.end(),print2());
cout << endl;
cout << "---------------------" << endl;
random_shuffle(v.begin(), v.end());
for_each(v.begin(), v.end(), print2());
}
int main()
{
srand((unsigned int)time(NULL));//随机数种子
test01();
//test02();
system("pause");
return 0;
}
10 14 14 14 20 26 50 67 75 89 153 167
---------------------
153 14 89 14 10 167 67 14 20 50 75 26 请按任意键继续. . .
3、merge
两个容器元素合并,并存储到另一个容器中
函数原型:
merge(iterator beg1,iterator end1,iterator beg2,iterator end2,iterator dest)
两个容器元素合并,并存储到另一个容器中
!!!注意:两个容器必须是有序的,并且顺序一致
beg1 容器1开始迭代器
end1 容器1结束迭代器
beg2 容器2开始迭代器
end2 容器2结束迭代器
dest 目标容器开始迭代器
void test01()
{
vector v;
v.push_back(10);
v.push_back(20);
v.push_back(50);
v.push_back(14);
v.push_back(67);
v.push_back(153);
v.push_back(167);
v.push_back(14);
v.push_back(26);
v.push_back(14);
v.push_back(89);
v.push_back(75);
sort(v.begin(), v.end());
for_each(v.begin(), v.end(),print2());
cout << endl;
cout << "---------------------" << endl;
vector v2;
v2.push_back(10);
v2.push_back(30);
v2.push_back(150);
v2.push_back(23);
v2.push_back(67);
v2.push_back(153);
v2.push_back(157);
v2.push_back(123);
v2.push_back(562);
v2.push_back(1235);
v2.push_back(845);
v2.push_back(777);
sort(v2.begin(), v2.end());
for_each(v2.begin(), v2.end(), print2());
cout << endl;
cout << "---------------------" << endl;
vector v3;
v3.resize(v.size()+v2.size());
merge(v.begin(), v.end(), v2.begin(), v2.end(), v3.begin());
for_each(v3.begin(), v3.end(), print2());
}
int main()
{
srand((unsigned int)time(NULL));
test01();
//test02();
system("pause");
return 0;
}
10 14 14 14 20 26 50 67 75 89 153 167
---------------------
10 23 30 67 123 150 153 157 562 777 845 1235
---------------------
10 10 14 14 14 20 23 26 30 50 67 67 75 89 123 150 153 153 157 167 562 777 845 1235 请按任意键继续. . .
4、reverse
将容器中元素进行反转
reverse(iterator beg,iterator end)
反转指定范围内元素
beg开始迭代器
end结束迭代器
void test01()
{
vector v;
v.push_back(10);
v.push_back(20);
v.push_back(50);
v.push_back(14);
v.push_back(67);
v.push_back(153);
v.push_back(167);
v.push_back(14);
v.push_back(26);
v.push_back(14);
v.push_back(89);
v.push_back(75);
sort(v.begin(), v.end());
for_each(v.begin(), v.end(),print2());
cout << endl;
cout << "---------------------" << endl;
reverse(v.begin(), v.end());
for_each(v.begin(), v.end(), print2());
}
int main()
{
srand((unsigned int)time(NULL));
test01();
//test02();
system("pause");
return 0;
}
10 14 14 14 20 26 50 67 75 89 153 167
---------------------
167 153 89 75 67 50 26 20 14 14 14 10 请按任意键继续. . .
四、常用拷贝和替换算法
算法简介:
1、copy
容器中指定范围的元素拷贝到另一个容器中
函数原型:
copy(iterator beg,iterator end,iterator dest);
beg开始迭代器
end结束迭代器
dest 目标起始迭代器
void test01()
{
vector v;
v.push_back(10);
v.push_back(20);
v.push_back(50);
v.push_back(14);
v.push_back(67);
v.push_back(153);
v.push_back(167);
v.push_back(14);
v.push_back(26);
v.push_back(14);
v.push_back(89);
v.push_back(75);
vector v2;
v2.resize(v.size());
copy(v.begin(), v.end(), v2.begin());
for_each(v2.begin(), v2.end(), print2());
}
int main()
{
srand((unsigned int)time(NULL));
test01();
//test02();
system("pause");
return 0;
}
2、replace
将容器内指定范围的旧元素修改为新元素
函数原型:
replace(iterator beg,iterator end,oldvalue,newvalue)
将容器内指定范围的旧元素修改为新元素
beg开始迭代器
end结束迭代器
old value 旧元素
newvalue 新元素
void test01()
{
vector v;
v.push_back(10);
v.push_back(20);
v.push_back(50);
v.push_back(14);
v.push_back(67);
v.push_back(153);
v.push_back(167);
v.push_back(14);
v.push_back(26);
v.push_back(14);
v.push_back(89);
v.push_back(75);
for_each(v.begin(), v.end(), print2());
cout << endl;
replace(v.begin(), v.end(), 14,10000);
for_each(v.begin(), v.end(), print2());
}
int main()
{
//srand((unsigned int)time(NULL));
test01();
//test02();
system("pause");
return 0;
}
10 20 50 14 67 153 167 14 26 14 89 75
10 20 50 10000 67 153 167 10000 26 10000 89 75 请按任意键继续. . .
3、replace_if
功能:将区间内满足条件的元素,替换成指定元素
函数原型:
replace_if(iterator beg,iterator end,pred_,newvalue)
按照条件替换元素,满足条件的替换成指定元素
beg开始迭代器
end结束迭代器
pred_谓词 (条件)
newvalue替换的新元素
class greatfive
{
public:
int operator()(int val)
{
return val > 20;
}
};
void test01()
{
vector v;
v.push_back(10);
v.push_back(20);
v.push_back(50);
v.push_back(14);
v.push_back(67);
v.push_back(153);
v.push_back(167);
v.push_back(14);
v.push_back(26);
v.push_back(14);
v.push_back(89);
v.push_back(75);
for_each(v.begin(), v.end(), print2());
cout << endl;
replace_if(v.begin(), v.end(), greatfive(),10000);
for_each(v.begin(), v.end(), print2());
}
int main()
{
//srand((unsigned int)time(NULL));
test01();
//test02();
system("pause");
return 0;
}
10 20 50 14 67 153 167 14 26 14 89 75
10 20 10000 14 10000 10000 10000 14 10000 14 10000 10000 请按任意键继续. . .
4、swap
功能:互换两个容器的元素
函数原型:
swap(container c1,container c2)
互换两个同种容器的元素
c1 容器1
c2 容器2
class print2
{
public:
void operator()(int val)
{
cout << val << " ";
}
};
void test01()
{
vector v;
v.push_back(10);
v.push_back(20);
v.push_back(50);
v.push_back(14);
v.push_back(67);
v.push_back(153);
v.push_back(167);
v.push_back(14);
v.push_back(26);
v.push_back(14);
v.push_back(89);
v.push_back(75);
for_each(v.begin(), v.end(), print2());
cout << endl;
vector v1;
v1.push_back(10);
v1.push_back(20);
v1.push_back(50);
v1.push_back(14);
v1.push_back(67);
v1.push_back(153);
for_each(v1.begin(), v1.end(), print2());
cout << endl;
swap(v, v1);
for_each(v.begin(), v.end(), print2());
cout << endl;
for_each(v1.begin(), v1.end(), print2());
}
int main()
{
//srand((unsigned int)time(NULL));
test01();
//test02();
system("pause");
return 0;
}
10 20 50 14 67 153 167 14 26 14 89 75
10 20 50 14 67 153
10 20 50 14 67 153
10 20 50 14 67 153 167 14 26 14 89 75 请按任意键继续. . .
五、常用算术生成算法
算术生成算法属于小型算法,使用时包含头文件为#include
算法简介:
accumulate 计算容器内元素累计总和
fill 向容器内添加元素
1、accumulate
功能:计算容器内元素累计总和
函数原型:
accumulate(iterator beg,iterator end,value)
beg 开始迭代器
end 结束迭代器
value 起始值
void test01()
{
vector v;
v.push_back(10);
v.push_back(20);
v.push_back(50);
v.push_back(14);
v.push_back(67);
v.push_back(153);
v.push_back(167);
v.push_back(14);
v.push_back(26);
v.push_back(14);
v.push_back(89);
v.push_back(75);
int sum = accumulate(v.begin(), v.end(), 0);
cout << sum << endl;
}
2、fill
函数原型:
fill(iterator beg,iterator end,value)
向容器中填充元素
beg 开始迭代器
end 结束迭代器
value 填充的值
void test01()
{
vector v;
v.push_back(10);
v.push_back(20);
v.push_back(50);
v.push_back(14);
v.push_back(67);
v.push_back(153);
v.push_back(167);
v.push_back(14);
v.push_back(26);
v.push_back(14);
v.push_back(89);
v.push_back(75);
for_each(v.begin(), v.end(), print2());
cout << endl;
fill(v.begin(), v.end(), 2);
for_each(v.begin(), v.end(), print2());
}
10 20 50 14 67 153 167 14 26 14 89 75
2 2 2 2 2 2 2 2 2 2 2 2 请按任意键继续. . .
六、常用集合算法
算法简介:
1、set_intersection
函数原型:
set_intersection(iterator beg1,iterator end1,iterator beg2,iterator end2,iterator beg3)
求两个集合的交集,返回目标容器的最后一个元素迭代器,两个集合必须是有序的
beg1,容器1开始迭代器
end1,容器1结束迭代器
beg2,容器2开始迭代器
end2,容器2结束迭代器
beg3,交集容器3开始迭代器
void test01()
{
vector v;
v.push_back(10);
v.push_back(20);
v.push_back(50);
v.push_back(14);
v.push_back(67);
v.push_back(153);
v.push_back(167);
v.push_back(14);
v.push_back(26);
v.push_back(14);
v.push_back(89);
v.push_back(75);
sort(v.begin(), v.end());
for_each(v.begin(), v.end(), print2());
cout << endl;
vector v1;
v1.push_back(10);
v1.push_back(20);
v1.push_back(50);
v1.push_back(14);
v1.push_back(67);
v1.push_back(153);
v1.push_back(167);
v1.push_back(15);
v1.push_back(17);
v1.push_back(100);
v1.push_back(101);
v1.push_back(102);
sort(v1.begin(), v1.end());
for_each(v1.begin(), v1.end(), print2());
cout << endl;
vector v2;
v2.resize(min(v.size(),v1.size()));
vector::iterator it = set_intersection(v.begin(), v.end(), v1.begin(), v1.end(), v2.begin());
for_each(v2.begin(), it, print2());
}
10 14 14 14 20 26 50 67 75 89 153 167
10 14 15 17 20 50 67 100 101 102 153 167
10 14 20 50 67 153 167 请按任意键继续. . .
2、set_union
函数原型:
set_union(iterator beg1,iterator end1,iterator beg2,iterator end2,iterator beg3)
求两个集合的并集,返回目标容器最后一个元素迭代器,两个集合必须是有序序列
beg1,容器1开始迭代器
end1,容器1结束迭代器
beg2,容器2开始迭代器
end2,容器2结束迭代器
beg3,交集容器3开始迭代器
void test01()
{
vector v;
v.push_back(10);
v.push_back(20);
v.push_back(50);
v.push_back(14);
v.push_back(67);
v.push_back(153);
v.push_back(167);
v.push_back(14);
v.push_back(26);
v.push_back(14);
v.push_back(89);
v.push_back(75);
sort(v.begin(), v.end());
for_each(v.begin(), v.end(), print2());
cout << endl;
vector v1;
v1.push_back(10);
v1.push_back(20);
v1.push_back(50);
v1.push_back(14);
v1.push_back(67);
v1.push_back(153);
v1.push_back(167);
v1.push_back(15);
v1.push_back(17);
v1.push_back(100);
v1.push_back(101);
v1.push_back(102);
sort(v1.begin(), v1.end());
for_each(v1.begin(), v1.end(), print2());
cout << endl;
vector v2;
v2.resize(v.size()+v1.size());
vector::iterator it = set_union(v.begin(), v.end(), v1.begin(), v1.end(), v2.begin());
for_each(v2.begin(), it, print2());
}
10 14 14 14 20 26 50 67 75 89 153 167
10 14 15 17 20 50 67 100 101 102 153 167
10 14 14 14 15 17 20 26 50 67 75 89 100 101 102 153 167 请按任意键继续. . .
3、set_difference
函数原型:
set_difference(iterator beg1,iterator end1,iterator beg2,iterator end2,iterator beg3)
求两个集合的差集,返回目标容器最后一个元素迭代器,两个集合必须是有序序列
beg1,容器1开始迭代器
end1,容器1结束迭代器
beg2,容器2开始迭代器
end2,容器2结束迭代器
beg3,交集容器3开始迭代器
void test01()
{
vector v;
v.push_back(10);
v.push_back(20);
v.push_back(50);
v.push_back(14);
v.push_back(67);
v.push_back(153);
v.push_back(167);
v.push_back(14);
v.push_back(26);
v.push_back(14);
v.push_back(89);
v.push_back(75);
sort(v.begin(), v.end());
for_each(v.begin(), v.end(), print2());
cout << endl;
vector v1;
v1.push_back(10);
v1.push_back(20);
v1.push_back(50);
v1.push_back(14);
v1.push_back(67);
v1.push_back(153);
v1.push_back(167);
v1.push_back(15);
v1.push_back(17);
v1.push_back(100);
v1.push_back(101);
v1.push_back(102);
sort(v1.begin(), v1.end());
for_each(v1.begin(), v1.end(), print2());
cout << endl;
vector v2;
v2.resize(v.size()+v1.size());
vector::iterator it = set_difference(v.begin(), v.end(), v1.begin(), v1.end(), v2.begin());
for_each(v2.begin(), it, print2());
}
10 14 14 14 20 26 50 67 75 89 153 167
10 14 15 17 20 50 67 100 101 102 153 167
14 14 26 75 89 请按任意键继续. . .