Bootstrap

【STL】7.STL常用算法(2)

前言

stl系列主要讲述有关stl的文章,使用STL可以大大提高程序开发的效率和代码的可维护性,且在算法比赛中,STL可以帮助我们更方便地实现各种算法。提高我们的效率。

简介

算法主要是头文件algorithm,functional,numeric组成
1.algorithm是所有STL头文件中最大的一个,范围涉及到比较,交换,查找,遍历操作,复制,修改等等
2.numeric体积很小,只包括几个在序列上面进行简单的数学运算的模板函数
3.functional定义了一些模板类,用以声明函数对象

四.常用拷贝和替换算法

copy();//将容器内指定范围的元素拷贝到另一个容器中
replace();//将容器指定范围的久元素修改为新元素
replace_if();//将容器指定范围内的久元素修改为新元素
swap();//交换两个容器的元素

1.copy

copy(first1,last,first2);//[first1,last)为被拷贝容器的范围,first2为拷贝到的容器起始位置

如:

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

void print(int val) {
	cout << val << " ";
}

int main() {
	vector<int> v1 = { 1,2,3,4,5 };
	vector<int> v2;
	v2.resize(v1.size());
	copy(v1.begin(), v1.end(), v2.begin());
	for_each(v2.begin(), v2.end(), print);

	return 0;
}

2.replace

replace(first,last,old,new);//在first到last范围内将old元素替换成new

如:

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

void print(int val) {
	cout << val << " ";
}

int main() {
	vector<int> v1 = { 1,2,3,4,5 };
	replace(v1.begin(), v1.end(), 1, 2);
	for_each(v1.begin(), v1.end(), print);

	return 0;
}

3.replace_if

repalce_if(first,last,f,new);//在first到last范围内将满足f元素替换成new

如:

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

void print(int val) {
	cout << val << " ";
}
bool f(int val) {
	return val > 2;
}

int main() {
	vector<int> v1 = { 1,2,3,4,5 };
	replace_if(v1.begin(), v1.end(), f, 2);
	for_each(v1.begin(), v1.end(), print);

	return 0;
}

4.swap

swap(c1,c2);//将容器c1和c2交换

如:

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

void print(int val) {
	cout << val << " ";
}

int main() {
	vector<int> v1 = { 1,2,3,4,5 };
	vector<int> v2 = { 4,5,6,7,8 };	
	swap(v1, v2);
	for_each(v1.begin(), v1.end(), print);
	cout << endl;
	for_each(v2.begin(), v2.end(), print);

	return 0;
}

五.算术生成算法

在头文件numeric中

accumulate();//计算容器元素累计总和
fill();//向容器添加元素

1.accumulate

accumulate(first,last,value);//在容器[first,last)范围内从值value开始计算总和

如:

#include <iostream>
#include <vector>
#include <numeric>
using namespace std;


int main() {
	vector<int> v1 = { 1,2,3,4,5 };
	int total = accumulate(v1.begin(), v1.end(), 2);
	cout << total << endl;

	return 0;
}

2.fill

fill(first,last,value);//在容器[first,last)范围内填充值value

如:

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

void print(int val) {
	cout << val << " ";
}


int main() {
	vector<int> v1 = { 1,2,3,4,5 };
	fill(v1.begin(), v1.end(), 2);
	for_each(v1.begin(), v1.end(), print);

	return 0;
}

六.常用集合算法

set_intersection();//求两个容器的交集
set_union();//求两个容器的并集
set_difference();//求两个容器的差集

1.set_intersection

set_intersection(first1,last1,first2,last2,first3);//first1,last1,first2,last2,为两个容器,first3为新容器

如:

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

void print(int val) {
	cout << val << " ";
}


int main() {
	vector<int> v1 = { 1,2,3,4,5 };
	vector<int> v2 = { 4,5,6,7,8 };
	vector<int> v3;
	v3.resize(min(v1.size(), v2.size()));
	set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());
	for_each(v3.begin(), v3.end(), print);

	return 0;
}

2.set_union

set_union(irst1,last1,first2,last2,first3);//first1,last1,first2,last2,为两个容器,first3为新容器

如:

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

void print(int val) {
	cout << val << " ";
}


int main() {
	vector<int> v1 = { 1,2,3,4,5 };
	vector<int> v2 = { 4,5,6,7,8 };
	vector<int> v3;
	v3.resize(v1.size() + v2.size());
	set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());
	for_each(v3.begin(), v3.end(), print);

	return 0;
}

3.set_difference

set_difference(first1,last1,first2,last2,first3);//first1,last1,first2,last2,为两个容器,first3为新容器

如:
v1 - v2

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

void print(int val) {
	cout << val << " ";
}


int main() {
	vector<int> v1 = { 1,2,3,4,5 };
	vector<int> v2 = { 4,5,6,7,8 };
	vector<int> v3;
	v3.resize(max(v1.size(), v2.size()));
	set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());
	for_each(v3.begin(), v3.end(), print);

	return 0;
}

v2 - v1

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

void print(int val) {
	cout << val << " ";
}


int main() {
	vector<int> v1 = { 1,2,3,4,5 };
	vector<int> v2 = { 4,5,6,7,8 };
	vector<int> v3;
	v3.resize(max(v1.size(), v2.size()));
	set_difference(v2.begin(), v2.end(), v1.begin(), v1.end(), v3.begin());
	for_each(v3.begin(), v3.end(), print);

	return 0;
}

总结

希望大家点赞收藏我会尽快更新STL!!!

;