本文共 5217 字,大约阅读时间需要 17 分钟。
编写程序,功能是查找一维数组a中的第一个值为x的元素的下标。如果不存在该元素就返回-1
functionTemplate.h文件
#ifndef _FUNCTIONTEMPLATE_H#define _FUNCTIONTEMPLATE_H#includeusing namespace std;template 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 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 void show(T a[],int n){ for(int i =0;i < n;i ++) { cout< <<" "; } cout<
#include#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<
类定义
Array.h文件
#ifndef _ARRAY_H#define _ARRAY_Husing namespace std;templateclass 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 void Array ::setElement(T x){ for(int i = 0 ;i < getLength();i ++) { buffer[i] = x; }}#endif // _ARRAY_H
main函数测试文件
#include#include "Array.h"using namespace std;int main(){ Array a; Array b; Array c; cout<<"the length of object a is:"< <
设计两个的函数模板,其功能分别是返回两个值中的较大者和返回多个值中的较大者。要求不仅能够处理整型、实型等数值性数据,而且能够省却处理字符串
#ifndef _FUNCTIONTEMPLATE_H#define _FUNCTIONTEMPLATE_H#includeusing namespace std;template T maxElement(T a,T b){ return a>b?a:b;}template T maxElement(T a[],int size){ cout<<"template T maxElement is being used!"< < size; i++) { max = maxElement(max,a[i]); } return max;}#endif
#include "functionTemplate.h"#includeusing namespace std;char *maxElement(char *a,char *b){ cout<<"char *maxElement(char *a,char *b) is being used"<
#include#include #include "functionTemplate.h"using namespace std;int main(){ int nArr[] = { 3,5,7,16,9,2}; double dArr[] = { 3.5,7.2,4.3,6.8}; char cArr[] = { 'a','b','c','d'}; char *sArr[] = { "Guolong","chening","wuhaolong"}; cout< <
描述:数组Array的实现
Arraytemplate.h文件
#ifndef _ARRAYTEMPLATE_#includeusing namespace std;template 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 initializationtemplate Array ::Array(int nSize){ m_nSize = nSize; m_pArray = new T[nSize + 1];}//destructor of the classtemplate Array ::~Array(){ delete []m_pArray;}//the overload of the operator[]template T& Array ::operator[](int index){ if(index >= m_nSize || index < 0) { cout<<"error:out of edge!"< void Array ::display(){ for(int i = 0;i < m_nSize;i ++) { cout< <<" "; } cout< bool Array ::addNewElement(int nAddSize){ if(nAddSize <= 0) { cout<<"the new array you add must over zero!"<
#include#include "Arraytemplate.h"using namespace std;int main(){ Array intArray(10); Array doubleArray(10); Array 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;
转载地址:http://dwwvb.baihongyu.com/