原创 linux下c++ lesson26 容器-算法

本文深入解析C++标准库中的关键算法,包括遍历、查找、排序、拷贝和函数适配器等,通过具体示例展示了如何在实际编程中应用这些算法,为读者提供了丰富的代码实践案例。

最后给大家整理个各种容器的意思及其用途
在这里插入图片描述

1-遍历算法.cpp

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

void show(int x)
{
	cout << x << endl;
}

class print
{
public:
	void operator()(int x)
	{
		cout << x << endl;
	}
};

int main()
{
	int array[5] = {1, 2, 3, 4, 5};
	vector<int> v(array, array + 5);

	//for_each遍历过程中不能修改数据
	for_each(v.begin(), v.end(), show);    //回调函数形式遍历
	for_each(v.begin(), v.end(), print()); //函数对象形式遍历 

	//transform遍历过程中可以修改数据
	string s("helloworld");
	transform(s.begin(), s.end(), s.begin(), ::toupper);
	cout << s << endl;

	return 0;
}

2-查找算法.cpp

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

bool GreaterTwo(int x)
{
	return x > 2;
}

class GreaterThree
{
public:
	bool operator()(int x)
	{
		return x > 3;
	}
};

int main()
{
	int array[6] = {1, 2, 2, 3, 4, 4};
	vector<int> v(array, array + 6);

	vector<int>::iterator it = adjacent_find(v.begin(), v.end());
	if (it == v.end())
	{
		cout << "不存在重复且相邻的" << endl;
	}
	else
	{
		cout << *it << endl;
	}

	bool ret = binary_search(v.begin(), v.end(), 4);      //在有序的序列里面查找
	if (ret)
	{
		cout << "元素存在" << endl;
	}
	else
	{
		cout << "元素不存在" << endl;
	}

	int num = count(v.begin(), v.end(), 2);
	cout << num << endl;

	num = count_if(v.begin(), v.end(), GreaterTwo);    //一元谓词   回调函数
	cout << num << endl;

	it = find(v.begin(), v.end(), 3);
	if (it == v.end())
	{
		cout << "元素不存在" << endl;
	}
	else
	{
		cout << *it << endl;
	}

	it = find_if(v.begin(), v.end(), GreaterThree());      //函数对象
	if (it == v.end())
	{
		cout << "元素不存在" << endl;
	}
	else
	{
		cout << *it << endl;
	}

	return 0;
}

3-排序算法.cpp

#include <iostream>
#include <vector>
#include <algorithm>
#include <stdlib.h>
#include <time.h>

using namespace std;

void show(int x)
{
	cout << x << " ";
}

int main()
{
	vector<int> v1;
	vector<int> v2;

	srand(time(NULL));
	for (int i = 0; i < 5; i++)
	{
		v1.push_back(rand() % 10);
		v2.push_back(rand() % 10);
	}

	sort(v1.begin(), v1.end(), less<int>());
	sort(v2.begin(), v2.end(), less<int>());

	cout << "v1:" << endl;
	for_each(v1.begin(), v1.end(), show);
	cout << endl;
	cout << "v2:" << endl;
	for_each(v2.begin(), v2.end(), show);
	cout << endl;

	vector<int> v3;
	v3.resize(10);
	merge(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());
	for_each(v3.begin(), v3.end(), show);
	cout << endl;

	random_shuffle(v1.begin(), v1.end());
	cout << "v1:" << endl;
	for_each(v1.begin(), v1.end(), show);
	cout << endl;
	
	reverse(v3.begin(), v3.end());
	for_each(v3.begin(), v3.end(), show);
	cout << endl;

	return 0;
}

4-拷贝替换.cpp

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

void show(int x)
{
	cout << x << " ";
}

int main()
{
	vector<int> v1(5, 1);
	vector<int> v2(5, 2);

	v2.resize(6);
	copy(v1.begin(), v1.end(), ++(v2.begin()));

	for_each(v2.begin(), v2.end(), show);
	cout << endl;

	swap(v1, v2);
	for_each(v2.begin(), v2.end(), show);
	cout << endl;

	return 0;
}

5-函数适配器.cpp

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

bool Equal(string str)
{
	return str == "bb";
}

int main()
{
	vector<string> v;

	v.push_back("aa");
	v.push_back("bb");
	v.push_back("cc");
	v.push_back("dd");

	//vector<string>::iterator it = find_if(v.begin(), v.end(), Equal);
	vector<string>::iterator it = find_if(v.begin(), v.end(),  bind1st(equal_to<string>(), "bb"));
	if (it == v.end())
	{
		cout << "不存在" << endl;
	}
	else
	{
		cout << *it << endl;
	}

	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值