C++的STL(持续更新)

4 篇文章 0 订阅

一. upper_bound()函数和 lower_bound()

1. upper_bound()

功能:
1.//查找[first, last)区域中第一个大于 val 的元素。

ForwardIterator upper_bound (ForwardIterator first, ForwardIterator last,const T& val);

详细解释:其中first和last表示的是区间地址的开始和结束位置,val表示的待查找的元素。整个函数表示从区间[first,last)中找到第一个大于val的元素。并返回该元素的地址,若不存在,则返回last。注意由于upper_bound()的底层实现是二分查找,所以使用该函数的前提是区间内元素已经排序好,且是从小到大排序。通过返回的地址减去起始地址first,可以得到第一个大于数字val的元素在区间中的位置(下标)。
例子:

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

int main()
{
	vector<int> nums{1,3,5,6};
	

	vector<int>::iterator a = upper_bound(nums.begin(), nums.end(), 3);
	cout << "第一个大于3的数是:" << *a;
	cout << "  它的下标是:" << a - nums.begin();


}

2.查找[first, last)区域中第一个不符合 comp 规则的元素

ForwardIterator upper_bound (ForwardIterator first, ForwardIterator last,const T& val, Compare comp);

若要在从大到小排序的数组中查找第一个小于val的地址,则需要在调用函数时传入多一个定义从大到小规则的“Compare”函数,否则,会返回错误的地址,注意,上面所说的“已排好序”,并不要求数据完全按照某个排序规则进行升序或降序排序,而仅仅要求 [first, last) 区域内所有令 element<val(默认)(或者 comp(element, val)成立的元素都位于不成立元素的前面(其中 element 为指定范围内的元素)

#include <iostream>     
#include <algorithm>   
#include <vector>       
using namespace std;
//以普通函数的方式定义查找规则
bool mycomp(int element, int val) { return element > val; }


int main() {

	vector<int> myvector1{ 4,6,3,1,2 };
	//根据 mycomp 规则,从 myvector 容器中找到第一个违背 mycomp 规则的元素
	vector<int>::iterator iter1 = upper_bound(myvector1.begin(), myvector1.end(), 3, mycomp);
	cout << "*iter1 = " << *iter1;

	
	vector<int> myvector2{ 5,4,3,1,2 };
	//查找第一个小与于4的值
	vector<int>::iterator iter2 = upper_bound(myvector2.begin(), myvector2.end(), 4, mycomp);
	cout << "   *iter2 = " << *iter2;
	return 0;
}

lower_bound()与 upper_bound() 的区别: lower_bound()查找[first, last)区域中第一个大于等于 val 的元素。其它的特性与 upper_bound() 一样,这里不再赘述。

二.优先队列

首先我们要知道的是优先队列它还是一个队列,所以队列的各种方法,在优先队列中同样适用,然后它和普通队列不同的是:它可以保证队首元素是整个队列的最大值或者是最小值。

#include <iostream>
#include <queue>
using  namespace std;

typedef pair<int, int> p;   

int main()
{
	priority_queue<int> pq;//定义一个最大优先队列
	pq.push(2);
	pq.push(4);
	pq.push(3);
	pq.push(1);
    std::cout << pq.top() << "\n";

	priority_queue<int,vector<int>, greater<int> > pq2;//定义一个最小优先队列
	pq2.push(2);
	pq2.push(4);
	pq2.push(3);
	pq2.push(1);
	std::cout << pq2.top()<<"\n";
	//使用复合数据类型时,当第一个数据相同时,
	//会比较第二个
	priority_queue<p, vector<p>, greater<p> > pq3;
	pq3.push({ 3,2 });
	pq3.push({ 3,1 });	
	std::cout << pq3.top().first <<","<< pq3.top().second << " ";
}

三. 全排列函数:next_permutation()函数和 prev_permutation()

1. next_permutation()

功能:对数组a中的前m~n个元素进行升序全排列,同时并改变原始数组的值。

bool next_permutation(iterator start,iterator end)

例子:

#include <iostream>
#include <algorithm>
using namespace std; 
int main()
{	
	int a[3]{ 1,2,3 };	
	int m=0,n=3;
	do
	{
		cout<<a[0]<<" "<<a[1]<<" "<<a[2]<<"\n";

	}while(next_permutation(a+m,a+n));
	 	
}

在这里插入图片描述

注意:next_permutation() 在使用前需要对欲排列数组按升序排序,否则只能找出该序列之后的全排列数。比如,如果数组num初始化为2,3,1,那么输出就变为了:
在这里插入图片描述
2. prev_permutation()
功能:对数组a中的前m~n个元素进行降序全排列,事先要对序列进行降序处理,用法和next_permutation基本一一致。

#include <iostream>
#include <algorithm>
using namespace std; 
int main()
{	
	int a[3]{ 3,2,1 };	
	int m=0,n=3;
	do
	{
		cout<<a[0]<<" "<<a[1]<<" "<<a[2]<<"\n";

	}while(prev_permutation(a+m,a+n));
	 	
}

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值