⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 12-11.cpp

📁 为初学者提供的最佳的C++程序设计源程序库
💻 CPP
字号:
#include<iostream.h>
#define Max 100
template<class T>
class Sample
{
  T A[Max];
  int n;
  public:
	  Sample(){}
	  Sample(Ta[],int i);
	  int seek(T c);
	  void disp()
	  {
	    for(int i=0;i<n;i++)
			cout<<A[i]<<" ";
		cout<<endl;
	  }
};
template<class T>
Sample<T>::Sample(T a[],int i)
{
  n=i;
  for(int j=0;j<i;j++)
	  A[j]=a[j];
}
template<class T>
int Sample<T>::seek(T c)
{
  int low=0,high=n-1,mid;
  while(low<=high)
  {
    mid=(low+high)/2;
	if(A[mid]==c)
		return mid;
	else if (A[mid]<c)low=mid+1;
	else high=mid-1;
  }
  return -1;
}
void main()
{
  char a[]="acegkmpwxz";
  Sample<char> s(a,10);
  cout<<"元素序列:";s.disp();
  cout<<"元素'g'的下标:"<<s.seek('g')<<endl;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -