用C++编写一个使用类模板对数组进行排序、查找和求元素和的程序
2个回答
展开全部
#include<iostream>
using namespace std;
//类模板
template<class T>
class vector
{
public:
void sort(T a[],int n);
T sum(T a[],int n);
int search(T e, T a[], int n);
};
//排序
template<class T>
void vector<T>::sort(T a[],int n)
{
T temp;
bool exchange;
for(int i = 1; i < n; i++)
{
exchange = false;
for(int j = n-1; j >= i; j--)
if(a[j] < a[j-1])
{
temp = a[j]; a[j] = a[j-1]; a[j-1] = temp;
exchange = true;
}
if(!exchange)
return;
}
}
//求和
template<class T>
T vector<T>::sum(T a[],int n)
{
T sum = a[0];
for(int i = 1; i < n; i++)
sum += a[i];
return sum;
}
//查找
template<class T>
int vector<T>::search(T e, T a[], int n)
{
for(int i = 0; i < n; i++)
if(a[i] == e)
return i;
return -1;
}
void main()
{
int data[5] = {5,4,3,2,1};
vector<int> obj;
cout<<"数组和为:"<<obj.sum(data,5)<<endl;
cout<<"数字4在数组中的位置是:"<<obj.search(4,data,5)<<endl;
cout<<"排序前数组各元素为:"<<endl;
for(int i = 0; i < 5; i++)
cout<<data[i]<<" ";
cout<<endl;
cout<<"排序后数组各元素为:"<<endl;
for(i = 0; i < 5; i++)
cout<<data[i]<<" ";
cout<<endl;
}
using namespace std;
//类模板
template<class T>
class vector
{
public:
void sort(T a[],int n);
T sum(T a[],int n);
int search(T e, T a[], int n);
};
//排序
template<class T>
void vector<T>::sort(T a[],int n)
{
T temp;
bool exchange;
for(int i = 1; i < n; i++)
{
exchange = false;
for(int j = n-1; j >= i; j--)
if(a[j] < a[j-1])
{
temp = a[j]; a[j] = a[j-1]; a[j-1] = temp;
exchange = true;
}
if(!exchange)
return;
}
}
//求和
template<class T>
T vector<T>::sum(T a[],int n)
{
T sum = a[0];
for(int i = 1; i < n; i++)
sum += a[i];
return sum;
}
//查找
template<class T>
int vector<T>::search(T e, T a[], int n)
{
for(int i = 0; i < n; i++)
if(a[i] == e)
return i;
return -1;
}
void main()
{
int data[5] = {5,4,3,2,1};
vector<int> obj;
cout<<"数组和为:"<<obj.sum(data,5)<<endl;
cout<<"数字4在数组中的位置是:"<<obj.search(4,data,5)<<endl;
cout<<"排序前数组各元素为:"<<endl;
for(int i = 0; i < 5; i++)
cout<<data[i]<<" ";
cout<<endl;
cout<<"排序后数组各元素为:"<<endl;
for(i = 0; i < 5; i++)
cout<<data[i]<<" ";
cout<<endl;
}
展开全部
#include <iostream>
#include <cstring>
#include <vector>
using namespace std;
template<typename T> class handle_array {
private:
T *arr;
int length;
public:
explicit handle_array()
{
}
handle_array(int n)
{
length = n;
arr = new T[n];
memset(arr, 0, sizeof(T)*n);
}
handle_array(T *p, int n)
{
length = n;
arr = new T[n];
for (int i=0; i<n; i++)
arr[i] = p[i];
}
void sort();
int locate(T x);
T sum();
void print();
virtual ~handle_array()
{
delete []arr;
}
};
template<typename T>
void handle_array<T>::sort()
{
T temp;
for (int i=0; i<length; i++)
for (int j=i+1; j<length; j++)
if (arr[i] > arr[j]) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
template<typename T>
int handle_array<T>::locate(T x)
{
int index = -1;
for (int i=0; i<length; i++)
if (arr[i] == x) {
index = i;
break;
}
return index;
}
template<typename T>
T handle_array<T>::sum()
{
T sum=0;
for (int i=0; i<length; i++)
sum += arr[i];
}
template<typename T>
void handle_array<T>::print()
{
for (int i=0; i<length; i++)
cout << arr[i] << ' ';
cout << endl;
}
int main()
{
int a[] = {1991, 8, 5};
double b[] = {1990, 8, 25};
handle_array<int> int_array(a, sizeof(a)/sizeof(a[0]));
int_array.sort();
int_array.print();
handle_array<double> double_array(b, sizeof(b)/sizeof(b[0]));
return 0;
}
#include <cstring>
#include <vector>
using namespace std;
template<typename T> class handle_array {
private:
T *arr;
int length;
public:
explicit handle_array()
{
}
handle_array(int n)
{
length = n;
arr = new T[n];
memset(arr, 0, sizeof(T)*n);
}
handle_array(T *p, int n)
{
length = n;
arr = new T[n];
for (int i=0; i<n; i++)
arr[i] = p[i];
}
void sort();
int locate(T x);
T sum();
void print();
virtual ~handle_array()
{
delete []arr;
}
};
template<typename T>
void handle_array<T>::sort()
{
T temp;
for (int i=0; i<length; i++)
for (int j=i+1; j<length; j++)
if (arr[i] > arr[j]) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
template<typename T>
int handle_array<T>::locate(T x)
{
int index = -1;
for (int i=0; i<length; i++)
if (arr[i] == x) {
index = i;
break;
}
return index;
}
template<typename T>
T handle_array<T>::sum()
{
T sum=0;
for (int i=0; i<length; i++)
sum += arr[i];
}
template<typename T>
void handle_array<T>::print()
{
for (int i=0; i<length; i++)
cout << arr[i] << ' ';
cout << endl;
}
int main()
{
int a[] = {1991, 8, 5};
double b[] = {1990, 8, 25};
handle_array<int> int_array(a, sizeof(a)/sizeof(a[0]));
int_array.sort();
int_array.print();
handle_array<double> double_array(b, sizeof(b)/sizeof(b[0]));
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询