C++中必考的SLT算法(代码量爆减的秘密)

算法头文件

小刘:亲情提示没有对应的头文件,算法是不能使用的哦

  • #include< algorithm > 是STL头文件最大的一个:涉及到 比较 查找 排序 遍历 复制 修改
  • #include< functional> 定义了一些模板类,以供使用
  • #include< numeric> 体积很小,只包括几个所学运算的模板函数

算法

宇哥:重点来了,注意啊

常用遍历算法(头文件algorithm)

  • for_each(iteraror begin,iterator end,_func)// 遍历容器 _func为函数
#include <iostream>
#include<vector>
#include<algorithm>
using namespace std;

void print(int n){
	cout << n << " ";
}
int main() {
	vector<int> t;
	t.push_back(10);
	t.push_back(20);
	t.push_back(30);
	t.push_back(40);
	t.push_back(50);
	t.push_back(60);
	for_each(t.begin(), t.end(), print);

}

在这里插入图片描述

  • transfrom(iterator begin1,iterator end1,iterator begin2,iterator end2,_func)//搬运一个容器到另一个容器

常用查找算法(头文件algorithm)

  • find(iterator begin,iterator end,_func)//寻找元素是否存在,一定要重载==, 返回迭代器
#include <iostream>
#include<vector>
#include<algorithm>
using namespace std;
class T {
public:
	int a;
	//在使用find 时 一定要重载==号
	bool operator==(const T& t) {
		if (this->a == t.a) {
			return true;
		}
		else
			false;
	}


};

int main() {
	vector<T> t;
	T t1, t2, t3;
	t1.a = 10;
	t2.a = 5;
	t3.a = 5;
	t.push_back(t1);
	t.push_back(t2);
	t.push_back(t3);

	//find 查找元素是否存在 返回迭代器
	vector<T>::iterator iter1 = find(t.begin(), t.end(), t1);
	if (iter1 != t.end()) {
	cout << iter1->a<<endl;//10
	}
	else {
		cout << "未找到该元素"<<endl;
	}


}

  • **find(iterator begin,iterator end,_Pred)//按 条件寻找元素,_Pred为函数或谓词

  • adjacent_find(iterator begin,iterator end,_Pred)查找是否有相邻重复元素存在
#include <iostream>
#include<vector>
#include<algorithm>
using namespace std;
class T {
public:
	int a;
	
	bool  operator=(const T& t) {
		if (this->a == t.a) {
			return true;
		}
		else
			false;
	}
};
class compare {
public:
	bool operator()(T t1, T t2) {
		if (t1.a == t2.a) {
			return true;
		}
		return false;
	}
};
int main() {
	vector<T> t;
	T t1, t2, t3;
	t1.a = 10;
	t2.a = 5;
	t3.a = 5;
	t.push_back(t1);
	t.push_back(t2);
	t.push_back(t3);

	//adjacent_find  寻找相邻重复元素
	vector<T>::iterator iter2 = adjacent_find(t.begin(), t.end(), compare());
	
	if (iter2 != t.end()) {
		cout << iter2->a << endl;//5
	}
	else {
		cout << "未找到相邻重复元素" << endl;
	}
}

  • binary_search(iterator begin,iterator end,_Pred)//二分法查找,容器必须有序

  • cout(iterator begin,iterator end,vaule)//查找元素个数
  • cout(iterator begin,iterator end,_Pred)//按条件查找元素个数

排序算法(头文件algorithm)

  • sort(iterator begin,iterator end,_Pred)//按照要求排序

  • random_shuffle(iterator begin,iterator end)//打乱容器内元素位置,随机洗牌

  • marge(iterator begin1,iterator end1,iterator begin2,iterator end2,iterator dest)//将两个容器合并到目标的容器里,目标容器需提前开辟空间

  • reserve(iterator begin,iterator end)//将容器中元素颠倒

拷贝替换算法(头文件algorithm)

拷贝过程中,须给新容器用resize提前开辟空间

  • copy(iterator begin,iterator end,iterator dest)//将一个容器中元素拷贝到另一个容器

  • replace(iterator begin,iterator end,oldvalue,newvalue)//将容器中旧元素改为新元素
  • replace_if(iterator begin,iterator end,_Pred,newvalue)//将容器中旧元素按条件改为新元素

  • swap(iterator begin1,iterator end1,iterator begin2,iterator end2)//互换两个容器元素

常用集合算法(头文件algorithm)

  • set_intersection(terator begin1,iterator end1,iterator begin2,iterator end2,iterator dest)//求两个容器的交集

  • set_union(terator begin1,iterator end1,iterator begin2,iterator end2,iterator dest)//求两个容器的并集

  • set_difference(terator begin1,iterator end1,iterator begin2,iterator end2,iterator dest)//求两个容器的差集

常用算数生成算法(头文件numeric)

  • accumulate(iterator begin,iterator end,value)//容器中元素累加 value 为起始累加值

  • fill(iterator begin,iterator end,value)//容器中用value填充

C++入门到此彻底结束,完结撒花,拜拜了你

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章