【无标题】

c++算法

在这里插入图片描述

#include <algorithm>
#include <vector>
#include <iostream>
#include <random>
using namespace std;

class likefun
{
public:
	void operator()(int i)
	{
		cout << i;
	}
};

void PrintV(int v)
{
	cout << v << " ";
}

int AddV(int v)
{
	return ++v;
}

bool large(int a)
{
	if (a > 5)
	{
		return true;
	}
	else
	{
		return false;
	}
}

void show(vector<int> v)
{
	for_each(v.begin(), v.end(), PrintV);
	cout << endl;
}

int myrandom (int i) { return std::rand()%i;}
int main()
{
	vector<int> v;//示例容器
	vector<int> new_v;//复制容器
	vector<int> reunion;//合并容器
	reunion.resize(30);
	vector<int>::iterator it;//返回迭代器
	bool flag;//返回的bool值
	int num = 0;//返回的下标
	new_v.resize(10);
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}

	//for_each
	for_each(v.begin(), v.end(), PrintV);//遍历,需要一个函数指针或者仿函数
	for_each(v.begin(), v.end(), likefun());
	cout << endl;
	//transform操作后放入新区域
	transform(v.begin(), v.end(), new_v.begin(), AddV);
	show(new_v);
	//find
	it = find(v.begin(), v.end(), 2);//查找,返回迭代器
	cout << *it << endl;
	cout << "---------------" << endl;

	//find_if
	it = find_if(v.begin(), v.end(), large);
	cout << *it << endl;
	//adjacent_find
	it = adjacent_find(v.begin(), v.end());
	cout << *it << endl;//因为vector中没有相同的,所以返回v.end()
	//binary_search
	flag = binary_search(v.begin(), v.end(), 10);
	cout << flag << endl;//二分查找,返回0和1
	//count
	num = count(v.begin(), v.end(),5);
	cout << num << endl;//查找等于这个元素的个数
	//count_if
	num = count_if(v.begin(), v.end(), large);
	cout << num << endl;//查找这个范围的个数
	cout << "---------------" << endl;

	//sort
	sort(v.begin(), v.end());
	show(v);
	//randon_shuffle
	random_shuffle(v.begin(), v.end(),myrandom);
	show(v);
	//merge
	merge(v.begin(), v.end(), new_v.begin(), new_v.end(),reunion.begin());
	show(reunion);//合并两个有序容器到一个新的空容器,注意,容器的容量要足够
	//reverse
	reverse(v.begin(), v.end());
	show(v);
	cout << "---------------" << endl;

	//replace
	replace(v.begin(), v.end(), 3, 300);
	show(v);//把某个数字替换为另外一个数字
	//replace_if
	replace_if(v.begin(), v.end(), large, 10);
	show(v);//交换某个范围的数字
	//swap
	swap(v[1],v[2]);
	show(v);//交换两个元素
	cout << "---------------" << endl;

	//fill
	fill(v.begin(), v.end(),10);
	show(v);//快速填充
	//copy
	copy(v.begin(), v.end(), reunion.begin());
	show(v);//拷贝容器
	cout << "---------------" << endl;

	//差集
	set_difference(v.begin(), v.end(), new_v.begin(), new_v.end(), reunion.begin());
	show(reunion);
	//交集
	set_intersection(v.begin(), v.end(), new_v.begin(), new_v.end(), reunion.begin());
	show(reunion);
	//并集
	set_union(v.begin(), v.end(), new_v.begin(), new_v.end(), reunion.begin());
	show(reunion);
	cout << "---------------" << endl;

	cout << "end" << endl;
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值