掌握了对数组元素的排序的方法,学会了对数组元素的查找的方法,还学习到了C++库实现查找和排序.
程序:
//Lab10_2.cpp
#include<iostream>
#include<algorithm>
using namespace std;
void displayArray(int a[],int n)
{ cout<<endl<<"你输入的数组是:";
for(int i=0;i<n;i++)cout<<a[i]<<" ";
cout<<endl;}
int main()
{ int n,j;
cout<<"请输入你要输入的数值的个数:";
cin>>n;
int *p;
p=new int[n];
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
for(int i=0;i<n;i++)
{ cout<<"输入第"<<i+1<<"个数值:"<<endl;
cin>>j;
p[i]=j;}
displayArray(p,n);
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
int l=1;
while(l)
{ int k;
int *q;
cout<<endl<<"输入你要查找的数值:";
cin>>k;
q=find(p,p+n-1,k);
if(q!=p+n-1)
cout<<"你查找的数值"<<k<<"的位置在第"<<q-p+1<<"位!"<<endl<<endl;
else
cout<<"你查找的数值"<<k<<"不存在!"<<endl<<endl;
cout<<"是否继续查找功能?"<<endl<<"是按1/否按0"<<endl;
cin>>l;
}return 0;}