标准模板库 algorithm(全部函数)

这篇博客详细介绍了C++标准模板库(STL)中的algorithm模块,包括各种操作如查找、复制、排序、比较、修改等函数的使用。通过这些函数,开发者可以提高代码的正确性和可读性。博客涵盖了从adjacent_find到upper_bound的多个算法,并举例说明其应用场景。
摘要由CSDN通过智能技术生成

目录

adjacent_find(查找相邻元素)

all_of(如果迭代器区间全部符合条件返回真,否则返回假)

any_of(如果迭代器区间有一个符合条件返回真,否则返回假)

 binary_search(二分查找,找到返回真,否则返回假)

copy(将一个区间,复制到目标迭代器位置)

copy_backward(将一个区间从尾部开始复制到目标迭代器位置)

 copy_if(将区间符合条件的元素复制到目标迭代器位置,返回目标的尾部迭代器)

copy_n(从迭代器开始复制前n个元素到目标迭代器开始位置)

count(返回迭代器区间与val相等的元素个数)

count_if(返回迭代器区间符合条件的元素个数)

equal(迭代器区间与目标迭代器位置开始进行比较,全部符合条件返回真,否则返回假)默认做相等比较

equal_range(迭代器区间与val相比较返回一个对组(上下限),默认比较规则为等于)

fill(在迭代器区间填充目标值)

fill_n(在迭代器位置填充几个目标值,C++11中返回指向填充元素的后一位置)

find(迭代器区间查找,查找val,找到返回一个匹配的元素,否则返回函数中的,结束迭代器)

find_end(查找匹配的迭代器区间,找到返回匹配的第一个迭代器,没找到返回结束迭代器)从后向前

find_first_of(查找区间中的匹配的任意一个元素,找到返回第一个匹配的迭代器,没找到返回函数内结束的迭代器)

find_if(按条件查找,返回第一次找到的迭代器,没有找到返回区间结尾)

find_if_not(按条件查找,返回第一次不匹配的迭代器,全部匹配返回区间结尾)

for_each(遍历)

generate(返回值赋值给迭代器区间)

generate_n(返回值赋值n个到迭代器指定位置)

includes(两个已排序的序列,进行区间比较,全部匹配返回真,否则返回假)就是排序规则相同时找子集

inplace_merge(将两个有序的,合并成一个有序的)归并排序

is_heap(判断是否为二叉堆,是返回真,不是返回假)堆排序

is_heap_until(返回迭代器区间内第一个破坏二叉堆结构元素的迭代器,都符合返回结尾迭代器)

is_partitioned(满足条件和不满足条件分为两部分,则返回真,否则返回假)

is_permutation(区间元素和目标元素向比,全部符合返回真,否则返回假.与位置无关)

is_sorted(如果按照规则有序则返回真,否则返回假)

is_sorted_until(返回第一个不按规则排序的元素迭代器,如果都按照规则排序返回结束迭代器)

iter_swap(交换两个容器迭代器指向的元素)

lexicographical_compare(按规则比较两个区间,返回bool)

lower_bound(返回>=的元素,不符合返回函数内结束迭代器)

make_heap(重新排列生成一个堆)

max(返回较大值)

max_element(返回区间最大值)

merge(两个有序按照规则合并成一个新的)

min(返回较小值)

minmax(返回一个对组,最大值和最小值)

minmax_element(返回一个对组,指向最大值最小值的迭代器,参数为迭代器区间)

min_element(返回一个指向最小值的迭代器,参数为区间)

mismatch(返回一个对组,两个不匹配的数字的迭代器)

move(将一个区间元素移动到指定位置,返回移动目标的末端迭代器)

move_backwrad(移动从后向前)

next_permutation(按照字典进行排列可以排列返回真,如果已是最大排列返回假)

none_of(如果区间内元素都不符合返回真,否则返回假)

nth_element(重迭代器位置开始分割,前后规则排列(但不是排序))

 partial_sort(局部排序,全部数据排序四个)

partial_sort_copy(局部排序后拷贝到一个容器中,排序前四个)

partition(将区间按照规则分割,返回分割的结尾迭代器)

partition_copy(将结果拷贝到两个容器)

partition_point(分割元素,返回分割元素第一部分的结尾迭代器)

pop_heap(将堆顶元素移动到范围之后,可以理解成弹出)

prev_parmutation(按照字典进行排列可以排列返回真,如果已是最大排列返回假)prev 321 排成 123 next 123 排成 321

push_heap(添加堆,需要先制作在添加然后调用,谓词要一致)

random_shuffle(将顺序打乱)

remove(移除元素,实际上并没有删除,返回指向删除元素的后一迭代器)

remove_copy(可以将移动后的拷贝到新容器中)

remove_copy_if(按规则可以将移动后的拷贝到新容器中)

 remove_if(按规则移除)

replace(替换)

replace_copy(替换拷贝到新的容器中)

replace_copy_if(按规则替换拷贝到新的容器中)

replace_if(按规则替换)

reverse(颠倒)

reverse_copy(颠倒复制到一个新的容器中)

rotate(这个东西咋说呢,就是按照一个迭代器位置旋转过去)

rotate_copy(旋转复制到一个新的容器中)

search(在一个区间中查找另一个区间,找到返回找到的迭代器,没找到返回结束迭代器)

search_n(在一个区间中查找连续几个几,找到返回找到的迭代器,没找到返回结束迭代器)

set_difference(差集)(一个区间和另一个区间相比另一个区间没有的元素,赋值到一个容器中,返回新构造容器的结束迭代器)

set_intersection(交集)

set_symmetric_difference(对称并集(不相同的元素))

set_union(并集)

shuffle(洗牌)

sort(排序)

sort_heap(堆排序)

stable_partition(分割返回分割的第一个迭代器(相同元素不会改变位置))

stable_sort(排序(相同元素不会改变位置))

swap(交换两个容器)

swap_ranges(可以按区间交换)

transform(将元素转换为规则元素)

unique(相邻元素按规则去重)

unique_copy(可以复制到新容器)

upper_bound(返回>的元素迭代器)


这个东西虽然函数众多但是我会把它搞全.比较开发中都是用他.

正确性有保障,代码的可读性也会大大增加.

由于太多了,文字叙述方面可能有些许问题.

头文件:#include <algorithm>

adjacent_find(查找相邻元素)

查找相邻的两个元素,可以用谓词改变查找规则.成功返回查找到第一个元素的迭代器,失败返回尾部迭代器.

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

using namespace std;

class Compare
{
public:
	bool operator()(int val, int val2)
	{
		return val > val2;
	}
};

int main()
{
	vector<int> v = { 1,2,3,4,4,5,7,6,9,8,8 };
	auto it = v.begin();

	it = adjacent_find(v.begin(), v.end());

	cout << *it << endl;	//4

	it = adjacent_find(it, v.end(), Compare());

	cout << *it << endl;	//7

	return 0;
}

all_of(如果迭代器区间全部符合条件返回真,否则返回假)

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

using namespace std;

int main()
{
	vector<int> v = { 1,2,3,4,5,6,7 };
	//[](int val) {return val > 0; }) lambda表达式 这东西也不知道怎么介绍 不过应该都能看懂吧.
	if (all_of(v.begin(), v.end(), [](int val) {return val > 0; }))
	{
		cout << "return val > 0" << endl;	//会执行
	}

	if (all_of(v.begin(), v.end(), [](int val) {return val > 1; }))
	{
		cout << "return val > 1" << endl;	//不会执行
	}

	return 0;
}

any_of(如果迭代器区间有一个符合条件返回真,否则返回假)

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

using namespace std;

int main()
{
	vector<int> v = { 1,2,3,4,5,6,7 };
	
	if (any_of(v.begin(), v.end(), [](int val) {return val == 7; }))
	{
		cout << "return val == 7" << endl;	//会执行
	}

	if (any_of(v.begin(), v.end(), [](int val) {return val < 0; }))
	{
		cout << "return val < 0" << endl;	//不会执行
	}

	return 0;
}

 binary_search(二分查找,找到返回真,否则返回假)

二分查找是啥东西之后在数据结构中说

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

using namespace std;

int main()
{
	//要使用二分查找必须是有序的序列
	vector<int> v = { 1,2,3,4,5,6,7 };

	if (binary_search(v.begin(), v.end(), 6))
	{
		cout << "found" << endl;
	}

	return 0;
}

copy(将一个区间,复制到目标迭代器位置)

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

using namespace std;

int main()
{
	vector<int> v2 = { 3,2,4,5,6,7,8,9 };
	int arr[] = { 1,2,3,4,5,6 };
	vector<int> v(v2.size());

	copy(v2.begin(), v2.end(), v.begin());
	for (auto& val : v)
	{
		cout << val << " ";
	}
	cout << endl;

	v.resize(sizeof(int[6]) / sizeof(int));
	copy(arr, arr + 6, v.begin());
	for (auto& val : v)
	{
		cout << val << " ";
	}
	cout << endl;

	return 0;
}

copy_backward(将一个区间从尾部开始复制到目标迭代器位置)

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

using namespace std;

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

	//v.resize(8);
	//copy_backward(arr, arr + 5, v.begin());	//会崩 从目标的尾部和区间尾部开始,从后向前复制

	v.resize(5);
	copy_backward(arr, arr + 3, v.end());
	for (auto& val : v)
	{
		cout << val << " ";	//12123
	}
	cout << endl;

	return 0;
}

 copy_if(将区间符合条件的元素复制到目标迭代器位置,返回目标的尾部迭代器)

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

using namespace std;

int main()
{
	vector<int> v;
	vector<int> v2 = { 1,2,3,4,5,6,7 };
	v.resize(v2.size());

	auto it = copy_if(v2.begin(), v2.end(), v.begin(), [](int val) {return val % 2; });
	v.resize(distance(v.begin(), it));	//重新分配元素个数
	for (auto& val : v)
	{
		cout << val << " ";	//1357
	}
	cout << endl;

	return 0;
}

copy_n(从迭代器开始复制前n个元素到目标迭代器开始位置)

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

using namespace std;

int main()
{
	vector<int> v;
	vector<int> v2 = { 1,2,3,4,5,6,7 };
	v.resize(v2.size());

	copy_n(v2.begin(), v2.size(), v.begin());
	for (auto& val : v)
	{
		cout << val << " ";	//1234567
	}
	cout << endl;

	return 0;
}

count(返回迭代器区间与val相等的元素个数)

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

using namespace std;

int main()
{
	vector<int> v = { 1,2,3,4,5,5,5,6,7 };

	int my_count = count(v.begin(), v.end(), 5);
	cout << my_count << endl;	//3

	return 0;
}

count_if(返回迭代器区间符合条件的元素个数)

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

using namespace std;

int main()
{
	vector<int> v = { 1,2,3,4,5,5,5,6,7 };

	int my_count = count_if(v.begin(), v.end(), [](int val) {return val % 2; });
	cout << my_count << endl;	//6

	return 0;
}

equal(迭代器区间与目标迭代器位置开始进行比较,全部符合条件返回真,否则返回假)默认做相等比较

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

using namespace std;

int main()
{
	vector<int> v = { 1,2,3,4,5,5,5,6,7 };
	vector<int> v2 = { 1,2,3,4,5,5,5,6,7 };
	vector<int> v3 = { 11,21,31,41,15,51,51,61,17 };

	if (equal(v.begin(), v.end(), v2.begin()))
	{
		cout << "equal(v.begin(), v.end(), v2.begin())" << endl;	//会执行
	}

	if (equal(v.begin(), v.end(), v3.begin(), [](int val, int val2) {return val < val2; }))
	{
		cout << "equal(v.begin(), v.end(), v3.begin(), [](int val, int val2) {return val < val2; })" << endl;	//会执行
	}

	return 0;
}

equal_range(迭代器区间与val相比较返回一个对组(上下限),默认比较规则为等于)

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

using namespace std;

int main()
{
	//必须有序
	vector<int> v = { 1,2,2,2,2,2,3,3,3,3,4,5,6 };
	auto ret = equal_range(v.begin(), v.end(), 2);
	cout << *ret.first << endl;	//2;
	cout << *ret.second << endl;	//3

	//3<1
	auto ret2 = equal_range(v.begin(), v.end(), 3, [](int val, int val2) {return val < val2; });
	cout << *ret2.first << endl;	//3
	cout << *ret2.second << endl;	//4

	return 0;
}

fill(在迭代器区间填充目标值)

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

using namespace std;

int main()
{
	vector<int> v(5);
	fill(v.begin(), v.end(), 11);
	for (auto& val : v)
	{
		cout << val << " ";	//11 11 11 11 11
	}
	cout << endl;

	return 0;
}

fill_n(在迭代器位置填充几个目标值,C++11中返回指向填充元素的后一位置)

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

using namespace std;

int main()
{
	vector<int> v = { 1,2,3,4,5 };
	fill_n(v.begin(), 3, 11);
	for (auto& val : v)
	{
		cout << val << " ";	//11 11 11 4 5
	}
	cout << endl;

	return 0;
}

find(迭代器区间查找,查找val,找到返回一个匹配的元素,否则返回函数中的,结束迭代器)

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

using namespace std;

int main()
{
	vector<int> v = { 1,2,3,4,5 };
	auto it = find(v.begin(), v.begin() + 4, 4);
	if (it != v.begin() + 4)
	{
		cout << *it << endl;	//4
	}

	return 0;
}

find_end(查找匹配的迭代器区间,找到返回匹配的第一个迭代器,没找到返回结束迭代器)从后向前

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

using namespace std;

int main()
{
	vector<int> v = { 1,2,3,4,5,1,2,3,4,5 };
	vector<int> v2 = { 2,3,4 };
	auto it = find_end(v.begin(), v.end(), v2.begin(), v2.end());
	if (it != v.end())
	{
		cout << *it << endl;	//2
		cout << it - v.begin() << endl;	//6
	}

	auto it2 = find_end(v.begin(), v.end(), v2.begin(), v2.end(), [](int val, int val2) {return val > val2; });

	if (it2 != v.end())
	{
		cout << *it2 << endl;	//3
		cout << it2 - v.begin() << endl;	//7
	}

	return 0;
}

find_first_of(查找区间中的匹配的任意一个元素,找到返回第一个匹配的迭代器,没找到返回函数内结束的迭代器)

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

using namespace std;

int main()
{
	vector<int> v = { 1,2,3,4,5 };
	vector<int> v2 = { 9,5,11 };
	auto it = find_first_of(v.begin(), v.end(), v2.begin(), v2.end());
	if (it != v.end())
	{
		cout << *it << endl;	//5
	}

	auto it2 = find_first_of(v.begin(), v.end(), v2.begin(), v2.end(), [](int val, int val2) {return ((val + 4) == val2); });
	if (it2 != v.end())
	{
		cout << *it2 << endl;	//1
	}

	return 0;
}

find_if(按条件查找,返回第一次找到的迭代器,没有找到返回区间结尾)

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

using namespace std;

int main()
{
	vector<int> v = { 1,2,3,4,5 };
	auto it = find_if(v.begin(), v.end(), [](int val) {return val > 3; });
	if (it != v.end())
	{
		cout << *it << endl;    //4
	}

	return 0;
}

find_if_not(按条件查找,返回第一次不匹配的迭代器,全部匹配返回区间结尾)

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

using namespace std;

int main()
{
	vector<int> v = { 1,2,3,4,5 };
	auto it = find_if_not(v.begin(), v.end(), [](int val) {return val > 3; });
	if (it != v.end())
	{
		cout << *it << endl;	//1
	}

	return 0;
}

for_each(遍历)

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

using namespace std;

int main()
{
	vector<int> v = { 1,2,3,4,5,6 };
	for_each(v.begin(), v.end(), [](int val) {cout << val << " "; });
	cout << endl;

	return 0;
}

generate(返回值赋值给迭代器区间)

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

using namespace std;

int main()
{
	vector<int> v(10);
	srand((unsigned int)time(NULL));

	generate(v.begin(), v.end(), []() {return rand() % 10; });
	for (auto& val : v)
	{
		cout << val << " ";	//随机的10个数字
	}
	cout << endl;

	return 0;
}

generate_n(返回值赋值n个到迭代器指定位置)

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

using namespace std;

int main()
{
	vector<int> v(10);
	srand((unsigned int)time(NULL));

	generate_n(v.begin(), 10, []() {return rand() % 10; });
	for (auto& val : v)
	{
		cout << val << " ";	//随机的10个数字
	}
	cout << endl;

	return 0;
}

includes(两个已排序的序列,进行区间比较,全部匹配返回真,否则返回假)就是排序规则相同时找子集

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

using namespace std;

int main()
{
	vector<int> v = { 1,2,3,4,5,6,7,8 };
	vector<int> v2 = { 2,3,4,5 };

	if (includes(v.begin(), v.end(), v2.begin(), v2.end()))
	{
		cout << "includes(v.begin(), v.end(), v2.begin(), v2.end())" << endl;
	}

	if (includes(v.begin(), v.end(), v2.begin(), v2.end(), [](int val, int val2) {return val < val2; }))
	{
		cout << "includes(v.begin(), v.end(), v2.begin(), v2.end(), [](int val, int val2) {return val < val2; })" << endl;
	}

	return 0;
}

inplace_merge(将两个有序的,合并成一个有序的)归并排序

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

using namespace std;

int main()
{
	//将两个有序的序列进行排序
	vector<int> v = { 1,2,4,6,8,0,2,5,7,9 };
	inplace_merge(v.begin(), v.begin() + 5, v.end());
	for (auto& val : v)
	{
		cout << val << " ";	//0 1 2 2 4 5 6 7 8 9
	}
	cout << endl;

	inplace_merge(v.begin(), v.begin() + 5, v.end(), [](int val, int val2) {return val < val2; });
	for (auto& val : v)
	{
		cout << val << " ";	//0 1 2 2 4 5 6 7 8 9
	}
	cout << endl;

	return 0;
}

is_heap(判断是否为二叉堆,是返回真,不是返回假)堆排序

二叉堆是什么之后在数据结构中说.

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

using namespace std;

int main()
{
	vector<int> v = { 1,2,3,4,5,6,7 };
	vector<int> v2 = { 7,6,5,4,3,2,1 };

	if (is_heap(v.begin(), v.end()))
	{
		cout << "is_heap(v.begin(), v.end()) is true" << endl;
	}
	else
	{
		cout << "is_heap(v.begin(), v.end()) is false" << endl;    //执行
	}

	if (is_heap(v.begin(), v.end(), [](int val, int val2) {return val > val2;}))
	{
		cout << "is_heap(v.begin(), v.end()) is true" << endl;    //执行
	}
	else
	{
		cout << "is_heap(v.begin(), v.end()) is false" << endl;
	}

	if (is_heap(v2.begin(), v2.end()))
	{
		cout << "is_heap(v2.begin(), v2.end()) is true" << endl;    //执行
	}
	else
	{
		cout << "is_heap(v2.begin(), v2.end()) is false" << endl;
	}

	return 0;
}

is_heap_until(返回迭代器区间内第一个破坏二叉堆结构元素的迭代器,都符合返回结尾迭代器)

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

using namespace std;

int main()
{
	vector<int> v = { 1,2,3,4,5,6,7 };
	vector<int> v2 = { 7,6,5,4,3,2,1 };

	auto it = is_heap_until(v.begin(), v.end());
	cout << *it << endl;	//2

	auto it2 = is_heap_until(v2.begin(), v2.end());
	cout << *(it2-1) << endl;	//1

	return 0;
}

is_partitioned(满足条件和不满足条件分为两部分,则返回真,否则返回假)

#include <iostream>
#inc
  • 9
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值