急!请各位大侠帮忙!!关于模版

lihh_cn 2001-11-19 09:13:22
设计一个类属函数 max_of_array( ),该函数从一个数组中找出其中的最大原素,数组中存放的元素类型可能是多种多样的。


一定要给出原程序!!!要有输入输出的完整的程序!!!感谢万分!
...全文
44 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
karma 2001-11-21
  • 打赏
  • 举报
回复
there is a function in:

#include <algorithem>
template<class InputIterator>
inline InputIterator max_element(InputIterator first, InputIterator last)

example from MSDN:


#pragma warning(disable: 4786)

#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
#include <string>

using namespace std;


void main()
{
const int VECTOR_SIZE = 8 ;

// Define a template class vector of int
typedef vector<int > IntVector ;

//Define an iterator for template class vector of strings
typedef IntVector::iterator IntVectorIt ;

IntVector Numbers(VECTOR_SIZE) ;

IntVectorIt start, end, it, location ;

// Initialize vector Numbers
Numbers[0] = 4 ;
Numbers[1] = 10;
Numbers[2] = 10 ;
Numbers[3] = 30 ;
Numbers[4] = 69 ;
Numbers[5] = 70 ;
Numbers[6] = 96 ;
Numbers[7] = 100;

start = Numbers.begin() ; // location of first
// element of Numbers

end = Numbers.end() ; // one past the location
// last element of Numbers

// print content of Numbers
cout << "Numbers { " ;
for(it = start; it != end; it++)
cout << *it << " " ;
cout << " }\n" << endl ;

// return the maximum element in the Numbers
location = max_element(start, end) ;

cout << "The maximum element in Numbers is: "
<< *location << endl ;


int i[4]={1,2,6,4};

int* iloc = max_element(i,i+4);

cout << "The maximum element in Numbers is: "
<< *iloc << endl ;


double d[6]={3.4,6,4,23.4,5,5.1133};
double*dloc = max_element(d,d+6);

cout << "The maximum element in Numbers is: "
<< *dloc << endl ;

string s[4]={"cc","aa","gg","dd"};

string* sloc = max_element(s,s+4);

cout << "The maximum element in Numbers is: "
<< *sloc << endl ;


}

jacklondon 2001-11-21
  • 打赏
  • 举报
回复
#include <iostream>
#include <string>
using namespace std;

class ClassA{
//..
public:
  template <class T>
  T max_of_array(const T *pArray,const int ArrayCount)
  {
    T max = (T)pArray[0];
    for(int i=0;i<ArrayCount;i++)
    {
      if(max < pArray[i])
        max = pArray[i];
    }
    return max;
  }
};


int main(int argc, char* argv[])
{
  ClassA a;
  int i[4]={1,2,6,4};
  double d[6]={3.4,6,4,23.4,5,5.1133};
  string s[4]={"cc","aa","gg","dd"};

  cout <<"max of int array: " << a.max_of_array(i,4) << endl;
  cout <<"max of double array: "<< a.max_of_array(d,6) << endl;
  cout <<"max of string array: " << a.max_of_array(s,4).c_str() << endl;

  return 0;
}

16,551

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Creator Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧