C++自学笔记

37 STL - 常用算法(03常用排序算法)

本次记录STL - 常用算法(03常用排序算法),还请各位大佬批评指正!

常用排序算法

目标: 掌握常用的排序算法。

算法简介:

  • sort //对容器内的元素进行排序
  • random_shuddle //洗牌 指定范围内的元素随机调整次序
  • merge //容器元素合并,并储存到另一容器中
  • reverse //反转指定范围的元素

sort

功能描述:
对容器内的元素进行排序。

函数原型:
sort(iterator beg,iteraotr end, _Pred);
//sort属于开发中最常用的算法之一,需熟练掌握
//beg 开始迭代器; end结束迭代器; _Pred 谓词

#include<iostream>
#include <stdlib.h>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;


//sort
void myPrint(int val)
{
	cout << val << " ";
}

void test01()
{
	vector<int>v;

	v.push_back(1);
	v.push_back(3);
	v.push_back(5);
	v.push_back(2);
	v.push_back(4);

	//利用sort进行升序
	sort(v.begin(), v.end());
	for_each(v.begin(), v.end(), myPrint);
	cout << endl;

	//利用sort进行降序
	sort(v.begin(), v.end(), greater<int>());
	for_each(v.begin(), v.end(), myPrint);
	cout << endl;
}

int main()
{
	test01();

	
	system("pause");

	return 0;
}

总结: 利用find可以在容器中找到指定的元素,但是返回的是迭代器。

random_shuffle

功能描述: 洗牌 指定范围内的元素随机调整次序。

函数原型:
random_shuffle(iterator beg,iteraotr end);
//指定范围内的元素随机调整次序
//beg 开始迭代器; end结束迭代器

#include<iostream>
#include <stdlib.h>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;
#include<functional>
#include<ctime>


//random_shuffle
void myPrint(int val)
{
	cout << val << " ";
}

void test01()
{
	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(), myPrint);
}

int main()
{
	test01();
	
	system("pause");

	return 0;
}

merge

功能描述: 两个容器元素合并,并储存到另一容器中。

函数原型:
merge(iterator beg1,iterator end1,iterator beg2,iterator end2,iterator dest);
//容器元素合并,并储存到另一容器中
//注意:两个容器必须是有序的,且同为升序或者降序
// beg1 容器1开始迭代器; end1 容器1结束迭代器
// beg2 容器2开始迭代器; end2 容器2结束迭代器;dest 目标容器开始迭代器

#include<iostream>
#include <stdlib.h>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;


//merge
void myPrint(int val)
{
	cout << val << " ";
}

void test01()
{
	// v1 v2 必须同时升序或者同时降序
	vector<int>v1;
	vector<int>v2;
	vector<int>vTarget;//目标容器

	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
		v2.push_back(i+1);
	}
	vTarget.resize(v1.size() + v2.size());//提前给目标容器分配内存

	merge(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());
	for_each(vTarget.begin(), vTarget.end(), myPrint);
	cout << endl;
}

void test02()
{
	vector<int>v1;
	vector<int>v2;
	vector<int>vTarget;//目标容器

	for (int i = 0; i < 10; i++)
	{
		v1.push_back(10 - i);
		v2.push_back(11 - i);
	}
	vTarget.resize(v1.size() + v2.size());//提前给目标容器分配内存

	merge(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin(),greater<int>());
	for_each(vTarget.begin(), vTarget.end(), myPrint);
	cout << endl;
}
int main()
{
	test01();
	test02();	
	
	system("pause");

	return 0;
}

reverse

功能描述: 将容器中的元素进行反转。

函数原型:
reverse(iterator beg, iterator end);
//反转指定范围的元素
//beg 开始迭代器; end 结束迭代器

#include<iostream>
#include <stdlib.h>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;

//reverse
//void myPrint(int val)
//{
//	cout << val << " ";
//}

class myPrint
{
public:
	void operator()(int val)
	{
		cout << val << " ";
	}
};

void test01()
{
	vector<int>v;

	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}
	cout << "反转前:" << endl;
	//for_each(v.begin(), v.end(), myPrint); //普通函数不加括号
	for_each(v.begin(), v.end(), myPrint()); //仿函数要加括号
	cout << endl;

	cout << "反转后:" << endl;
	reverse(v.begin(), v.end());
	//for_each(v.begin(), v.end(), myPrint);
	for_each(v.begin(), v.end(), myPrint());
	cout << endl;
}
int main()
{
	test01();
	
	system("pause");

	return 0;
}

38 STL - 常用算法(04常用拷贝和替换算法)

本次记录STL - 常用算法(04常用拷贝和替换算法),还请各位大佬批评指正!

常用拷贝和替换算法

目标: 掌握常用的拷贝和替换和算法。

算法简介:

  • copy //容器内指定范围的元素拷贝到另一容器中
  • replace //将容器内指定范围的旧元素修改为新元素
  • replace_if //容器内指定范围满足条件的元素替换为新元素
  • swap //互换两个容器的元素

copy

功能描述:
容器内指定范围的元素拷贝到另一容器中。

函数原型:
copy(iterator beg,iterator end,iterator dest);
//beg 原容器开始迭代器; end 原容器结束迭代器; dest 目标容器开始迭代器
//注意:利用copy算法在拷贝时,目标容器需要提前开辟空间

#include<iostream>
#include <stdlib.h>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;


//copy
void myPrint(int val)
{
	cout << val << " ";
}

void test01()
{
	vector<int>v1;
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
	}

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

	copy(v1.begin(), v1.end(), v2.begin());
	for_each(v2.begin(), v2.end(), myPrint);
	cout << endl;
}

int main()
{
	test01();

	system("pause");

	return 0;
}

总结: 利用find可以在容器中找到指定的元素,但是返回的是迭代器。

replace

功能描述: 将容器内指定范围的旧元素修改为新元素。

函数原型:
replace(iterator beg,iterator end,oldvalue, newvalue);
//beg 开始迭代器; end 结束迭代器;
//oldvalue 旧元素; newvalue 新元素

#include<iostream>
#include <stdlib.h>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;

//replace
class myPrint
{
public:
	void operator()(int val)
	{
		cout << val << " ";
	}
};

void test01()
{
	vector<int>v;

	v.push_back(10);
	v.push_back(20);
	v.push_back(20);
	v.push_back(20);
	v.push_back(20);
	v.push_back(20);
	v.push_back(10);

	cout << "替换前:" << endl;
	for_each(v.begin(), v.end(), myPrint());
	cout << endl;

	cout << "替换后:" << endl;
	replace(v.begin(), v.end(), 20, 60); //20替换为60
	for_each(v.begin(), v.end(), myPrint());
	cout << endl;
}

int main()
{
	test01();
	
	system("pause");

	return 0;
}

replace_if

功能描述: 将区间内满足条件的元素,替换成指定元素。

函数原型:
replace_if(iterator beg,iterator end, _Pred, newvalue);
//按条件替换元素,满足条件的替换成指定元素
//beg 开始迭代器; end 结束迭代器;
//_Pred 谓词; newvalue 新元素
总结: replace_if按条件查找,可以利用仿函数灵活筛选满足的条件。

#include<iostream>
#include <stdlib.h>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;


//replace_if
class myPrint
{
public:
	void operator()(int val)
	{
		cout << val << " ";
	}
};

class Smaller60
{
public:
	bool operator()(int val)
	{
		return val < 60;
	}
};

void test01()
{
	vector<int>v;

	v.push_back(10);
	v.push_back(30);
	v.push_back(40);
	v.push_back(20);
	v.push_back(50);
	v.push_back(60);

	cout << "替换前:" << endl;
	for_each(v.begin(), v.end(), myPrint());
	cout << endl;

	//将小于60的替换为60
	replace_if(v.begin(), v.end(), Smaller60(), 60);
	cout << "替换后:" << endl;
	for_each(v.begin(), v.end(), myPrint());
	cout << endl;
}

int main()
{
	test01();	
	
	system("pause");

	return 0;
}

swap

功能描述: 互换两个同种类型的容器的元素。

函数原型:
swap(container c1,contanier c2);
// c1 容器1; c2 容器2

#include<iostream>
#include <stdlib.h>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;

//swap
class myPrint
{
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 + 100);
	}

	cout << "交换前:" << endl;
	for_each(v1.begin(), v1.end(), myPrint());
	cout << endl;
	for_each(v2.begin(), v2.end(), myPrint());
	cout << endl;

	cout << "交换后:" << endl;
	swap(v1, v2);
	for_each(v1.begin(), v1.end(), myPrint());
	cout << endl;
	for_each(v2.begin(), v2.end(), myPrint());
	cout << endl;
}

int main()
{
	test01();
	
	system("pause");

	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值