STL大法之二分函数

## 致歉:

本文使用 Markdown 格式,想要体验更好的阅读感受可以将文章复制在支持 Markdown 格式的地方(可以复杂的叭,皮一下QwQ)。

## 更正:
**文中所有函数都有区间,在此修改并提醒。**

——$(2024-6-7)$

**初版有许多问题,如将 $fill(a+first,a+last,x)$ 写成 $fill(a,a+10,x)$,在此改正。**

——$(2024-6-7)$

**修改了部分错别字。**

——$(2024-6-7)$

**改正了 $lower$_$bound(a+first,a+last+1,y)$ 使用解释的一些问题。**

——$(2024-7-15)$ 左右

**有点大改,不过本身内容不太多还能接受,修正了格式,将部分表述改清晰了。**

——$(2024-7-23)$
## 说明:
当使用有关二分的函数时区间自动认为有序。非声明的 $last$ 为该数组下标,区间为 $a+first$ 到$a+last$。阅读请先了解 $sort(a+first,a+last+1)$(快速排序)及用法。

$lu 2023$ 原创。
## 正文:
## $fill(a+first,a+last,x)$
**作用:将数组 $a$ 中区间 $[first,last]$ 中所有元素赋值为 $x$。**

**实例:**
```cpp
#include<bits/stdc++.h>
using namespace std;

int main(){
    int a[10];
    fill(a,a+10,1);
    for(int i=0;i<10;i++)
        cout<<a[i]<<" "; 
    return 0;
}     
```
**效果:**
```cpp
1 1 1 1 1 1 1 1 1 1
```
## $partial$_$sort(a+x,a+y+1,a+last+1)$
**作用:将 $a$ 数组中的 $x$ 到 $y$ 排序。**

**实例:**
```cpp
#include<bits/stdc++.h>
using namespace std;

int main(){
    int a[]={8,7,6,5,4,3,2,1};
    partial_sort(a,a+3,a+8);
    for(int i=0;i<8;i++)
        cout<<a[i]<<" "; 
    return 0;
}     
```
**效果:**
```cpp
1 2 3 8 7 6 5 4
```
## $nth$_$element(a+first,a+num,a+last)$
**作用:将数组 $a$ 中区间 $[first,last]$ 的一个第 $num+1$ 大的数放在第 $num$ 个位置,这时数组会被打乱。**

**实例:**
```cpp
#include<bits/stdc++.h>
using namespace std;
int a[]={8,7,6,5,4,3,2,1};
int main(){
    nth_element(a,a+3,a+8);
    for(int i=0;i<8;i++)
        cout<<a[i]<<" ";
    return 0;
}     
```
**效果:**
```cpp
2 1 3 4 8 5 6 7
```
## $binary$_$search(a+first,a+last+1,y)$
**作用:二分寻找数组 $a$ 中区间 $[first,last]$ 中是否有 $y$。**

**实例1:**
```cpp
#include<bits/stdc++.h>
using namespace std;
int a[]={8,7,6,5,4,3,2,1};
int main(){
    sort(a,a+8);
    if(binary_search(a,a+8,5))
        cout<<"find";
    else
        cout<<"no";
    return 0;
}     
```
**效果:**
```cpp
find
```
**实例2:**
```cpp
#include<bits/stdc++.h>
using namespace std;
int a[]={8,7,6,5,4,3,2,1};
int main(){
    sort(a,a+8);
    if(binary_search(a,a+8,10))
        cout<<"find";
    else
        cout<<"no";
    return 0;
}     
```
**效果:**
```cpp
no
```
## $lower$_$bound(a+first,a+last+1,y)$
## $and$ 
## $upper$_$bound(a+first,a+last+1,y)$
$lower$_$bound(a+first,a+last+1,y)$
**作用:返回区间内第一个大于等于 $y$ 的数的地址。**

**实例:**
```cpp
#include<bits/stdc++.h>
using namespace std;
int a[]={8,7,6,5,4,3,2,1};
int main(){
    sort(a,a+8);
    cout<<lower_bound(a,a+8,2)-a;
    return 0;
}     
```
**效果:**
```cpp
1
```
$upper$_$bound(a+first,a+last+1,y)$**作用:返回区间 $a$ 内第一个大于 $y$ 的数的地址。**

**实例:**
```cpp
#include<bits/stdc++.h>
using namespace std;
int a[]={8,7,6,5,4,3,2,1};
int main(){
    sort(a,a+8);
    cout<<upper_bound(a,a+8,2)-a;
    return 0;
}     
```
**效果:**
```cpp
2
```
**共同使用可以求出区间内 $y$ 的个数:**
```cpp
#include<bits/stdc++.h>
using namespace std;
int a[]={8,8,7,6,5,4,3,2,1};
int main(){
    sort(a,a+9);
    cout<<upper_bound(a,a+9,8)-lower_bound(a,a+9,8);
    return 0;
}     
```
**效果:**
```cpp
2
```
## $merge(a+x1,a+y1,b+x2,b+y2,c)$
**作用:将数组 $a$ 中有序区间 $[x1,y1]$ 与数组 $b$ 中有序区间 $[x2,y2]$ 合并至数组 $c$ 中。**

**实例:**
```cpp
#include<bits/stdc++.h>
using namespace std;
int a[]={1,3,5,7,9};
int b[]={2,4,6,8,10};
int c[10]; 
int main(){
    merge(a,a+5,b,b+5,c);
    for(int i=0;i<10;i++)
        cout<<c[i]<<" ";
    return 0;
}     
```
**效果:**
```cpp
1 2 3 4 5 6 7 8 9 10
```
## $unique(a+first,a+last)$
**作用:将有序数组 $a$ 中区间内重复的数去掉并返回去掉后的长度。**

**实例:**
```cpp
#include<bits/stdc++.h>
using namespace std;
int a[]={1,1,3,4,5,6,8,8,8};
int len;
int main(){
    sort(a,a+9);
    int len=unique(a,a+8)-a;
    for(int i=0;i<len;i++)
        cout<<a[i]<<" ";
    return 0;
}
```
**效果:**
```cpp
1 3 4 5 6 8
```
## $reverse(a+first,a+last+1)$
**作用:将数组 $a$ 中区间 $[first,last]$ 中的数字顺序翻转。**

**实例:**
```cpp
#include<bits/stdc++.h>
using namespace std;
int a[]={8,7,6,5,4,3,2,1};
int main(){
    reverse(a,a+8);
    for(int i=0;i<8;i++)
        cout<<a[i]<<" ";
    return 0;
}
```
**效果:**
```cpp
1 2 3 4 5 6 7 8
```

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值