c++标准库中的sort()函数,可以直接进行对一直数组进行排序。另外还有库函数lower_bound()可以直接实现查找一个元素在有序数组中第一次出现的位置。和(upper_bound())最后一次出现的下一个位置。
实例如下:
#include<iostream>
#include<algorithm>
//#include<string>
using namespace std;
int v[10000];
void main()
{
int n,m,a,b;
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++)
scanf("%d",&v[i]);//输入数组元素
sort(v,v+n); //对数组元素进行排序
for(int i=0;i<m;i++)
{
scanf("%d%d",&a,&b);//下面将使用内部查找函数进行查找
printf("%d\n",upper_bound(v,v+n,b)-lower_bound(v,v+n,a));//找[a,b]范围内整数的个数
}
}