常用算法(学习笔记)

1.遍历算法(for_each,transform) 

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
//普通函数
void print01_for_each(int val)
{
	cout << val << " ";
}
//仿函数
class print02_for_each
{
public:
	void operator()(int val)
	{
		cout << val << " ";
	}
};
//利用for_each遍历容器
void test_for_each()
{
	vector<int> v;
	for (int i=0; i < 10; i++)
	{
		v.push_back(i);
	}
	//利用普通函数
	for_each(v.begin(), v.end(), print01_for_each);
	cout << endl;
	//利用仿函数
	for_each(v.begin(), v.end(), print02_for_each());
	cout << endl;
}

//利用transform遍历容器

//仿函数
class Transform
{
public:
	int operator()(int v)
	{
		return v;
	}
};
void test_transform()
{
	vector<int> v0;
	for (int i = 0; i < 10; i++)
	{
		v0.push_back(i);
	}

	vector<int> v1;//目标容器
	v1.resize(v0.size());//目标容器需要提前开辟空间
	//将v0转移到v1
	transform(v0.begin(), v0.end(), v1.begin(), Transform());//按照需求来改变逻辑规则Transform()
	for_each(v1.begin(), v1.end(), print02_for_each()); cout << endl;
}
int main()
{
	//test_for_each();
	test_transform();
	return 0;
}

find   //查找元素

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
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 test_find()
{
	//内置数据类型
	vector<int> v;
	for (int i=0; i < 10; i++)
	{
		v.push_back(i);
	}
	vector<int>::iterator pos = find(v.begin(), v.end(), 5);
	if (pos != v.end())
	{
		cout << "找到" << *pos << endl;
	}
	else
	{
		cout << "未找到" << endl;
	}
	
	//自定义数据类型Person
	vector<Person>vp;
	Person p1("aaa", 11);
	Person p2("bbb", 22);
	Person p3("ccc", 33);
	Person p4("ddd", 44);

	vp.push_back(p1);
	vp.push_back(p2);
	vp.push_back(p3);
	vp.push_back(p4);

	vector<Person>::iterator Ppos=find(vp.begin(), vp.end(), p2);//因为没有可以进行比较的函数,所以重载==运算符
	if (Ppos == vp.end())
	{
		cout << "未找到" << endl;
	}
	else
	{
		cout << "找到:" << Ppos->m_name << " " << Ppos->m_age << endl;
	}
}



int main()
{
	test_find();

	return 0;
}

find_if  //按条件查找

#include <iostream>
#include <vector>
#include <algorithm>
#include<string>
using namespace std;
class Person
{
public:
	string m_name;
	int m_age;
	Person(string name, int age)
	{
		this->m_name = name;
		this->m_age = age;
	}
};
//内置数据类型谓词仿函数(大于5)
class FIve
{
public:
	bool operator()(int val)
	{
		return val > 5;
	}
};

//自定义数据类型谓词仿函数(大于30)
class FIND30
{
public:
	bool operator()(const Person& p)
	{
		return p.m_age > 30;
	}
};
void test_find_if()
{
	//内置数据类型
	vector<int> v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}

	vector<int>::iterator pos = find_if(v.begin(), v.end(), FIve());

	for (; pos != v.end(); pos++)
	{
		if (pos != v.end())
		{
			cout << "找到" << *pos << endl;
		}
		else
		{
			cout << "未找到" << endl;
		}
	}

	//自定义数据类型Person
	vector<Person>vp;
	Person p1("aaa", 11);
	Person p2("bbb", 22);
	Person p3("ccc", 33);
	Person p4("ddd", 44);

	vp.push_back(p1);
	vp.push_back(p2);
	vp.push_back(p3);
	vp.push_back(p4);

	vector<Person>::iterator Ppos = find_if(vp.begin(), vp.end(), FIND30());
	if (Ppos == vp.end())
	{
		cout << "未找到" << endl;
	}
	else
		for (; Ppos != vp.end(); Ppos++)
		{

			cout << "找到:" << Ppos->m_name << " " << Ppos->m_age << endl;
		}
}
int main()
{
	test_find_if();

	return 0;
}


adjacent——find   //查找相邻重复元素

#include <iostream>
#include <vector>
#include <algorithm>
#include<string>
using namespace std;
void test_find_if()
{
	//内置数据类型
	vector<int> v;
	v.push_back(2);
	v.push_back(1);
	v.push_back(3);
	v.push_back(5);
	v.push_back(6);
	v.push_back(3);
	v.push_back(3);


	vector<int>::iterator pos = adjacent_find(v.begin(), v.end());
	if (pos != v.end())
	{
		cout << "找到相邻重复元素" << *pos << endl;
	}
	else
	{
		cout << "未找到相邻重复元素" << endl;
	}

}
int main()
{
	test_find_if();

	return 0;
}


binary_search    //二分法查找(返回值为ture或false)(在有序序列查找)

#include <iostream>
#include <vector>
#include <algorithm>
#include<string>
using namespace std;
void test_binary_search()
{
	//内置数据类型
	vector<int> v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}

	bool pos = binary_search(v.begin(), v.end(),6);//必须有序 且返回值为true或false
	if (pos)
	{
		cout << "找到元素" <<endl;
	}
	else
	{
		cout << "未找到元素" << endl;
	}

}
int main()
{
	test_binary_search();

	return 0;
}


count   //统计元素个数

#include <iostream>
#include <vector>
#include <algorithm>
#include<string>
using namespace std;
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_age == p.m_age)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
};
void test_count()
{
	//内置数据类型
	vector<int> v;
	v.push_back(1);
	v.push_back(2);
	v.push_back(3);
	v.push_back(3);
	v.push_back(5);
	v.push_back(6);

	int num = count(v.begin(), v.end(), 3);
	cout <<"共"<< num<<"个" << endl;

	//自定义数据类型Person
	vector<Person>vp;
	Person p1("aaa", 11);
	Person p2("bbb", 22);
	Person p3("ccc", 33);
	Person p4("ddd", 44);
	Person p5("eee", 44);
	Person p6("fff", 44);

	vp.push_back(p1);
	vp.push_back(p2);
	vp.push_back(p3);
	vp.push_back(p4);
	vp.push_back(p5);
	vp.push_back(p6);
	Person p("***", 44);
	int num2 = count(vp.begin(), vp.end(),p);//需要重载==
	cout <<num2<< endl;
}
int main()
{
	test_count();

	return 0;
}


count_if   //按条件统计元素个数

#include <iostream>
#include <vector>
#include <algorithm>
#include<string>
using namespace std;
class Person
{
public:
	string m_name;
	int m_age;
	Person(string name, int age)
	{
		this->m_name = name;
		this->m_age = age;
	}
};
//谓词 return大于3
class Greater
{
public:
	bool operator()(int val)
	{
		return val > 3;
	}
};

//谓词年龄大于33
class Greaterage
{
public:
	bool operator()(const Person& p)
	{
		return p.m_age > 30;
	}
};
void test_count_if()
{
	//内置数据类型
	vector<int> v;
	v.push_back(1);
	v.push_back(2);
	v.push_back(3);
	v.push_back(3);
	v.push_back(5);
	v.push_back(6);

	int num = count_if(v.begin(), v.end(), Greater());
	cout <<"大于3的个数:"<< num << endl;

	//自定义类型
	vector<Person>vp;
	Person p1("aaa", 11);
	Person p2("bbb", 22);
	Person p3("ccc", 33);
	Person p4("ddd", 44);
	Person p5("eee", 44);
	Person p6("fff", 55);

	vp.push_back(p1);
	vp.push_back(p2);
	vp.push_back(p3);
	vp.push_back(p4);
	vp.push_back(p5);
	vp.push_back(p6);
	int num2 = count_if(vp.begin(), vp.end(), Greaterage());//统计大于30岁人数
	cout << "年龄大于30的个数:" << num2 << endl;
}
int main()
{
	test_count_if();

	return 0;
}

3.排序算法

sort();   //对容器内元素进行排序

#include <iostream>
#include <vector>
#include <algorithm>
#include<string>
using namespace std;
//输出打印
class print
{
public:
	void operator()(int val)
	{
		cout << val << " ";
	}
};
void test_sort()
{
	
	vector<int> v;
	v.push_back(1);
	v.push_back(2);
	v.push_back(7);
	v.push_back(3);
	v.push_back(5);
	v.push_back(6);

	//默认排序
	sort(v.begin(), v.end());
	//遍历
	for_each(v.begin(), v.end(), print()); cout << endl;
	
	//降序排列,利用内建函数对象
	sort(v.begin(), v.end(),greater<int>());
	//遍历
	for_each(v.begin(), v.end(), print()); cout << endl;
}
int main()
{
	test_sort();

	return 0;
}


random_shuffle();   //洗牌 在指定范围内的元素随机调整次序

#include <iostream>
#include <vector>
#include <algorithm>
#include<string>
#include<ctime>
using namespace std;
//输出打印
class print
{
public:
	void operator()(int val)
	{
		cout << val << " ";
	}
};
void test_sort()
{
	//加入随机种子
	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(), print()); cout << endl;
}
int main()
{
	test_sort();

	return 0;
}


merge();   //容器元素合并,并存储到另一容器上

#include <iostream>
#include <vector>
#include <algorithm>
#include<string>
#include<ctime>
using namespace std;
//输出打印
class print
{
public:
	void operator()(int val)
	{
		cout << val << " ";
	}
};
void test_merge()
{
	vector<int>v1;
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
	}
	vector<int>v2;
	for (int i = 15; i <20; i++)
	{
		v2.push_back(i);
	}
	vector<int>v3;
	v3.resize(v1.size() + v2.size());
	merge(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());//v1 v2必须是有序序列 合并后v3也是有序序列
	//遍历
	for_each(v3.begin(), v3.end(), print()); cout << endl;
	
}
int main()
{
	test_merge();

	return 0;
}


reverse();   /反转指定范围的元素

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
//输出打印
class print
{
public:
	void operator()(int val)
	{
		cout << val << " ";
	}
};
void test_merge()
{
	vector<int>v1;
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
	}
	reverse(v1.begin(), v1.end());
	//遍历
	for_each(v1.begin(), v1.end(), print()); cout << endl;
	
}
int main()
{
	test_merge();

	return 0;
}

4.常用拷贝和替换算法:

copy();   //容器内指定范围的元素拷贝到另一个容器中(使用赋值更好)

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
//输出打印
class print
{
public:
	void operator()(int val)
	{
		cout << val << " ";
	}
};
void test_merge()
{
	vector<int>v1;
	vector<int>v2;
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
	}
	v2.resize(v1.size());
	copy(v1.begin(), v1.end(), v2.begin());
	//遍历
	for_each(v2.begin(), v2.end(), print()); cout << endl;
	
}
int main()
{
	test_merge();

	return 0;
}


replace();   将容器内指定范围内的旧元素修改为新元素

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
//输出打印
class print
{
public:
	void operator()(int val)
	{
		cout << val << " ";
	}
};
void test_merge()
{
	vector<int>v1;
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
	}
	
	//替换部分元素
	replace(v1.begin(), v1.end(), 6, 66);
	//遍历
	for_each(v1.begin(), v1.end(), print()); cout << endl;
	
}
int main()
{
	test_merge();

	return 0;
}


replace_if();  //容器内指定范围满足条件的元素替换为新元素

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
//输出打印
class print
{
public:
	void operator()(int val)
	{
		cout << val << " ";
	}
};
//仿函数 大于5替换为999
class Greater999
{
public:
	bool operator()(int val)
	{
		return val > 5;
	}
};
void test_merge()
{
	vector<int>v1;
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
	}
	
	//替换部分元素
	replace_if(v1.begin(), v1.end(), Greater999(), 999);
	//遍历
	for_each(v1.begin(), v1.end(), print()); cout << endl;
	
}
int main()
{
	test_merge();

	return 0;
}


swap();   //互换两个容器的元素

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
//输出打印
class print
{
public:
	void operator()(int val)
	{
		cout << val << " ";
	}
};

void test_merge()
{
	vector<int>v1;
	vector<int>v2;
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
		v2.push_back(i + 2);
	}
	//遍历
	for_each(v1.begin(), v1.end(), print()); cout << endl;
	for_each(v2.begin(), v2.end(), print()); 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()
{
	test_merge();

	return 0;
}

5.算法生成算法属于小型算法,使用时包含头文件#include<numeric>

accumulate   //计算容器元素累计总和

#include <iostream>
#include <vector>
#include <algorithm>
#include<numeric>
using namespace std;
void test01()
{
	vector<int>v1;
	for (int i = 0; i <= 100; i++)
	{
		v1.push_back(i);
	}
	int num=accumulate(v1.begin(), v1.end(),0); //参数三 是起始的累加值 例如:1000--6050
	cout << num << endl;
	
}
int main()
{
	test01();

	return 0;
}

fill   //向容器添加元素

#include <iostream>
#include <vector>
#include <algorithm>
#include<numeric>
using namespace std;
//输出打印
class print
{
public:
	void operator()(int val)
	{
		cout << val << " ";
	}
};

void test01()
{
	vector<int>v1;
	v1.resize(10);//给10个大小 元素为0
	fill(v1.begin(), v1.end(), 100);//将十个数改成100
	for_each(v1.begin(), v1.end(), print()); cout << endl;
}
int main()
{
	test01();

	return 0;
}

6.集合算法

set_interesectiom   //求两个容器的交集

#include <iostream>
#include <vector>
#include <algorithm>
#include<numeric>
using namespace std;
//输出打印
class print
{
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(i + 7);
	}
	vector<int>v;
	//利用min()算法求一个最小值
	v.resize(min(v1.size(), v2.size()));
	vector<int>::iterator itend=set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), v.begin());//求两个容器中的交集,返回最后一个元素的位置
	for_each(v.begin(), itend, print());
}
int main()
{
	test01();

	return 0;
}

set_union   //求两个容器的并集

#include <iostream>
#include <vector>
#include <algorithm>
#include<numeric>
using namespace std;
//输出打印
class print
{
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(i + 7);
	}
	vector<int>v;
	//利用min()算法求一个最小值
	v.resize(v1.size()+ v2.size());
	vector<int>::iterator itend=set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), v.begin());//求两个容器中的并集,返回最后一个元素的位置
	for_each(v.begin(), itend, print());
}
int main()
{
	test01();

	return 0;
}


set_difference    //求两个容器的差集

#include <iostream>
#include <vector>
#include <algorithm>
#include<numeric>
using namespace std;
//输出打印
class print
{
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(i + 7);
	}
	vector<int>v;
	//利用min()算法求一个最大值,提前开辟空间
	v.resize(max(v1.size(),v2.size()));
	vector<int>::iterator itend=set_difference(v2.begin(), v2.end(), v1.begin(), v1.end(), v.begin());//求两个容器中的差集,返回最后一个元素的位置
	//(v1,v2)求出的是v1和v2的差集 ; (v2,v1)求v2和v1的差集
	for_each(v.begin(), itend, print());//结束使用 最后一个元素的迭代器
}
int main()
{
	test01();

	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值