C++ STL 常用算法

STL算法太多了, 我这里整理几个常用的

#include <iostream>
#include <algorithm>
#include <vector>
#include <list>
#include <time.h>
#include <functional>
#include <numeric>
using namespace std;


void printC(vector<int> &v)
{
	for(vector<int>::iterator it = v.begin(); it<v.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

template <typename T>
class ShowE
{
public:
	void operator()(T t)
	{
		cout << t << " ";
	}
};


// 遍历算法
// for_each
void func6_1()
{
	vector <int> v;
	v.push_back(1);
	v.push_back(3);
	v.push_back(5);
	v.push_back(7);
	v.push_back(9);
	printC(v);

	for_each(v.begin(), v.end(), ShowE<int>());
	cout << endl;
}

int increase(int a)
{
	return a+100;
}


// 转换算法
// transfrom
void func6_2()
{
	vector<int> v;
	v.push_back(1);
	v.push_back(3);
	v.push_back(5);
	v.push_back(7);
	v.push_back(9);

	// transform 需要的函数要有返回值, for_each 需要的参数是引用 不需要返回值
	transform(v.begin(), v.end(), v.begin(), increase);
	printC(v);

	// 取反
	transform(v.begin(), v.end() , v.begin(), negate<int>());
	printC(v);

	// 放到别的容器中
	list<int> ls;
	ls.resize(v.size());

	//transform(v.begin(), v.end(), ls.begin(), increase);
	transform(v.begin(), v.end() , ls.begin(), bind2nd(multiplies<int>(), 100));
	for_each(ls.begin(), ls.end(), ShowE<int>());
	cout << endl;
}

// 查找算法
// adjacent_find
void func6_3()
{
	vector<int> v;
	v.push_back(1);
	v.push_back(3);
	v.push_back(5);
	v.push_back(5);
	v.push_back(7);
	v.push_back(7);
	v.push_back(9);

	// 在容器中查找是否有重复的元素, 返回第一个重复的元素的迭代器
	// 这里 输出 5
	vector<int> :: iterator it = adjacent_find(v.begin(), v.end());
	if(it != v.end())
		cout << *it << endl;
}

//binary_search
void func6_4()
{
	vector<int> v;
	v.push_back(1);
	v.push_back(3);
	v.push_back(5);
	v.push_back(5);
	v.push_back(7);
	v.push_back(9);

	// binary_search 二分法查找, 注意: 只可以在有序数据中查找, 不然没用
	bool b = binary_search(v.begin(), v.end(), 7);
	if(b)
		cout << "找到" << endl;
	else 
		cout << "没有找打" << endl;
}


// 排序算法
// merge 合并排序
// random_shuffle 随机排序
// reverse 倒置排序
void func6_5()
{
	vector<int> v1;
	v1.push_back(1);
	v1.push_back(3);
	v1.push_back(5);

	vector<int> v2;
	v2.push_back(2);
	v2.push_back(4);
	v2.push_back(6);

	vector<int> v3;
	v3.resize(v1.size() + v2.size());

	merge(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());
	printC(v3);

	srand((unsigned int)time(NULL));

	// 随机排序, 需要设置随机种子
	random_shuffle(v3.begin(), v3.end());
	printC(v3);

	// 倒置排序
	reverse(v3.begin(), v3.end());
	printC(v3);

	// 复制算法
	vector<int> v4(v3.size()); // 默认全是0
	copy(v3.begin(), v3.begin()+5, v4.begin()); 
	v4[2] = 0;
	v4[3] = 0;
	printC(v4);

	// 替换算法
	// 把零全部换成10
	replace(v4.begin(), v4.end(), 0 ,10);
	printC(v4);

	replace_if(v4.begin(), v4.end(), bind2nd(greater<int>(), 2), 7);
	printC(v4);

	// 交换算法  可以用于清空容器
	vector<int> v5;
	swap(v3,v5);

	cout << "v3:" << endl;
	printC(v3);

	printC(v4);
	// 求和算法
	int sum = accumulate(v4.begin(), v4.end(), 100);
	cout << sum << endl;

	// 区域改变
	// 注意  这只之改变两个元素
	fill(v4.begin(), v4.begin()+2, 100);
	printC(v4);
}






int main()
{
	//func6_1();
	//func6_2();
	//func6_3();
	//func6_4();
	func6_5();
	system("pause");
	return 0;
}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值