50 排序的工程应用示例
原文:https://www.cnblogs.com/wanmeishenghuo/p/9688158.html 参考狄泰软件相关教程
我们要使Srot能排序Array数组类。
Sort应该既能排序静态数组类又能排序动态数组类。
这个函数返回原生数组的首地址。
数组类需要新增成员函数array,排序类需要新增六个静态成员函数。
Array.h添加array函数:
#ifndef ARRAY_H
#define ARRAY_H
#include "Object.h"
#include "Exception.h"
namespace DTLib
{
template <typename T>
class Array : public Object
{
protected:
T* m_array;
public:
virtual bool set(int i, const T&e) //O(1)
{
bool ret = ((0 <= i) && (i < length()));
if( ret )
{
m_array[i] = e;
}
return ret;
}
virtual bool get(int i, T& e) const //O(1)
{
bool ret = ((0 <= i) && (i < length()));
if( ret )
{
e = m_array[i];
}
return ret;
}
T& operator[] (int i) //O(1)
{
if((0 <= i) && (i < length()))
{
return m_array[i];
}
else
{
THROW_EXCEPTION(IndexOutOfBoundsException, "Parameter i is invalid ...");
}
}
T operator[] (int i) const //O(1)
{
return (const_cast<Array<T>>(*this))[i];
}
T* array()const
{
return m_array;
}
virtual int length() const = 0;
};
}
#endif // ARRAY_H

![50 排序的工程应用示例
[编程语言教程]](https://www.zixueka.com/wp-content/uploads/2024/01/1706710179-3922739e7fe9573.jpg)


