编写程序,功能是查找一维数组a中的第一个值为x的元素的下标。如果不存在该元素就返回-1
functionTemplate.h文件
#ifndef _FUNCTIONTEMPLATE_H #define _FUNCTIONTEMPLATE_H #include <iostream> using namespace std; template <typename T> T maxElement(T a,T b) { return a>b?a:b; } /* description:search the element x in the array a. return the index where it firstly appear. */ template <typename T> int findElement(T a[],T x,int n) { for(int i = 0;i < n;i ++) { if(a[i] == x) return i; } return -1; } /* description:show different type of array */ template <typename T> void show(T a[],int n) { for(int i =0;i < n;i ++) { cout<<a[i]<<" "; } cout<<endl; } #endif // _FUNCTIONTEMPLATE_H main测试文件 #include <iostream> #include "functionTemplate.h" using namespace std; int main() { int a[6]; double b[6]; char c[6]; for(int i = 0;i < 6; i++) { a[i] = i; b[i] = i; c[i] = 'a' + i; } show(a,6); cout<<endl; show(b,6); cout<<endl; show(c,6); cout<<endl; cout<<findElement(a,3,6)<<endl; cout<<findElement(b,(double)3,6)<<endl; cout<<findElement(c,'d',6)<<endl; return 0; }类定义 类体外的成员函数的定义
使用
Array.h文件
#ifndef _ARRAY_H #define _ARRAY_H using namespace std; template <typename T,int length> class Array { private: T buffer[length]; public: T getElement(int i) { return buffer[i]; } int getLength() { return sizeof(buffer)/sizeof(T); } void setElement(T x); }; template <typename T,int length> void Array<T,length>::setElement(T x) { for(int i = 0 ;i < getLength();i ++) { buffer[i] = x; } } #endif // _ARRAY_Hmain函数测试文件
#include <iostream> #include "Array.h" using namespace std; int main() { Array<int,10> a; Array<double,20> b; Array<char,30> c; cout<<"the length of object a is:"<<a.getLength()<<endl; cout<<"the length of object b is:"<<b.getLength()<<endl; cout<<"the length of object c is:"<<c.getLength()<<endl; a.setElement(100); b.setElement(12.5); c.setElement('\n'); cout<<"the third element fo the array a is :"<<a.getElement(3)<<endl; cout<<"the third element fo the array b is :"<<b.getElement(3)<<endl; cout<<"the third element fo the array c is :"<<c.getElement(3)<<endl; return 0; }设计两个的函数模板,其功能分别是返回两个值中的较大者和返回多个值中的较大者。要求不仅能够处理整型、实型等数值性数据,而且能够省却处理字符串
描述:数组Array的实现
成员:数组的指针 和 数组的实际长度成员函数:带参数的构造函数(参数为对应的长度)、析构函数、下标符号的重载(返回特定索引的元素引用)、输出当前的元素和增加数组的长度Arraytemplate.h文件
#ifndef _ARRAYTEMPLATE_ #include <iostream> using namespace std; template <class T> class Array { private: T *m_pArray; int m_nSize; public: Array(int nSize); ~Array(); T& operator[](int index); int Size(){return m_nSize;} bool addNewElement(int nAddSize); void display(); }; //construct the array without any initialization template<class T> Array<T>::Array(int nSize) { m_nSize = nSize; m_pArray = new T[nSize + 1]; } //destructor of the class template <class T> Array<T>::~Array() { delete []m_pArray; } //the overload of the operator[] template <class T> T& Array<T>::operator[](int index) { if(index >= m_nSize || index < 0) { cout<<"error:out of edge!"<<endl; //use the superfluous space to store the illegal value; index = m_nSize; m_pArray[index] = 0; } return m_pArray[index]; } //show the array template <class T> void Array<T>::display() { for(int i = 0;i < m_nSize;i ++) { cout<<m_pArray[i]<<" "; } cout<<endl; } //mix the two array together template <class T> bool Array<T>::addNewElement(int nAddSize) { if(nAddSize <= 0) { cout<<"the new array you add must over zero!"<<endl; return false; } T *pNewArray = new T[m_nSize + nAddSize]; if(pNewArray == NULL) { cout<<"Error:arrange the memory absortively"<<endl; return false; } for(int i = 0 ;i < m_nSize;i ++) { pNewArray[i] = m_pArray[i]; } delete []m_pArray; m_pArray = pNewArray; m_nSize = m_nSize + nAddSize; return true; } #endif // _ARRAYTEMPLATE_ main测试文件 #include <iostream> #include "Arraytemplate.h" using namespace std; int main() { Array<int> intArray(10); Array<double> doubleArray(10); Array<char> charArray(10); for(int i = 0;i < 10; i++) { intArray[i] = 1 + i; doubleArray[i] = 2.5 + i; charArray[i] = 'a' + i; } intArray.display(); doubleArray.display(); charArray.display(); intArray[10] = 20; intArray.display(); intArray.addNewElement(1); intArray[10] = 20; intArray.display(); return 0; 测试结果