這個(gè)主要是給我自己看的,記錄一下語法的使用,也可以參照了解,我已經(jīng)用C++和OC代碼測試過這兩個(gè)文件了,如果寫的有什么不足,歡迎指出
文件Array.hpp
#ifndef Array_hpp
#define Array_hpp
#include <stdio.h>
#include <iostream>
using namespace std;
namespace GYJ{
template <class T>
class Array {
private:
static const int DEFAULT_CAPACITY = 10;
int m_size; //元素個(gè)數(shù)
T *arr; //數(shù)組
int m_capacity; //容量
inline void cheakIndexBeforeAdd(int index); //添加時(shí)的驗(yàn)證index
inline void cheakIndexBeforeSearch(int index); //查找時(shí)的驗(yàn)證index
void expandCapacity(int size); //擴(kuò)容
void reduceCapacity(); //縮容
public:
Array(); //構(gòu)造函數(shù)
Array(const int capacity); //帶參構(gòu)造函數(shù)
~Array(); //析構(gòu)函數(shù)
int size(); //獲取數(shù)組元素個(gè)數(shù)
void add(T item); //添加元素
T removeIndex(int index); //移除元素
T elmentAtIndex(int index);
void insert(int index,T item); //插入元素
T operator[](int index); //下標(biāo)
};
template <class T>
Array<T>::Array() :Array(DEFAULT_CAPACITY){}
template <class T>
Array<T>::Array(const int capacity){
m_capacity = capacity < DEFAULT_CAPACITY ? DEFAULT_CAPACITY : capacity;
m_size = 0;
arr = new T[m_capacity];
}
template <class T>
Array<T>::~Array(){
if (arr == NULL) return;
delete[] arr;
arr = NULL;
}
template <class T>
int Array<T>::size(){
return m_size;
}
template <class T>
void Array<T>::add(T item){
insert(m_size, item);
}
template <class T>
T Array<T>::removeIndex(int index){
cheakIndexBeforeSearch(index);
T tmp = arr[index];
for (int i = index + 1; i < m_size ; i++) {
arr[i-1] = arr[i];
}
m_size--;
reduceCapacity();
return tmp;
}
template <class T>
void Array<T>::insert(int index,T item){
cheakIndexBeforeAdd(index);
expandCapacity(m_size + 1);
for (int i = m_size - 1; i > index; i--) {
arr[i+1] = arr[i];
}
arr[index] = item;
m_size++;
}
template <class T>
T Array<T>::operator[](int index){
cheakIndexBeforeSearch(index);
return arr[index];
}
template <class T>
T Array<T>::elmentAtIndex(int index){
cheakIndexBeforeSearch(index);
return arr[index];
}
template <class T>
inline void Array<T>::cheakIndexBeforeAdd(int index){
if (index < 0 || index > m_size) {
throw "數(shù)組越界";
}
}
template <class T>
inline void Array<T>::cheakIndexBeforeSearch(int index){
if (index < 0 || index > m_size - 1) {
throw "數(shù)組越界";
}
}
template <class T>
void Array<T>::expandCapacity(int size){
if(size <= m_capacity) return;
m_capacity *= 1.8;
T *tempArr = new T[m_capacity];
for (int i = 0; i < m_size; i++) {
tempArr[i] = arr[i];
}
delete [] arr;
arr = tempArr;
}
template <class T>
void Array<T>::reduceCapacity(){
if (m_size > (m_capacity >> 1)) return;
m_capacity = (m_capacity>>1) < DEFAULT_CAPACITY ? DEFAULT_CAPACITY : m_capacity>>1;
T *tmpArr = new T[m_capacity];
for (int i = 0; i < m_size; i++) {
tmpArr[i] = arr[i];
}
delete [] arr;
arr = tmpArr;
}
template <class T>
ostream& operator<<(ostream &os,Array<T> &arr){
os << "[";
int size = arr.size();
for (int i = 0; i < size; i++) {
if(i != 0){
os << ",";
}
try{
os << arr[i];
}catch(...){
}
}
os << "]";
return os;
}
}
#endif
頭文件GYJMutableArray.h,供OC測試和使用的
NS_ASSUME_NONNULL_BEGIN
@interface GYJMutableArray : NSObject
- (int)size;
- (GYJMutableArray *)initWithCapacity:(NSInteger)capacity;
- (void)addElement:(NSObject *)element;
- (void)insertElement:(NSObject *)element atIndex:(NSInteger)index;
- (NSObject *)elementAtIndex:(NSInteger)index;
- (NSObject *)removeAtIndex:(NSInteger)index;
@end
NS_ASSUME_NONNULL_END
實(shí)現(xiàn)文件GYJMutableArray.mm,后綴名為mm是可以兼容c++和OC代碼的
#import "GYJMutableArray.h"
#include "Array.hpp"
using namespace GYJ;
@interface GYJMutableArray()
{
@private
Array<NSObject *> *_mArray;
}
@end
@implementation GYJMutableArray
- (instancetype)init {
if (self = [super init]) {
_mArray = new Array<NSObject *>();
}
return self;
}
- (GYJMutableArray *)initWithCapacity:(NSInteger)capacity {
if (self = [super init]) {
_mArray = new Array<NSObject *>((int)capacity);
}
return self;
}
- (void)addElement:(NSObject *)element {
_mArray->add(element);
}
- (void)insertElement:(NSObject *)element atIndex:(NSInteger)index {
_mArray->insert((int)index,element);
}
- (NSObject *)elementAtIndex:(NSInteger)index {
NSObject *obj = _mArray->elmentAtIndex((int)index);;
return obj;
}
- (NSObject *)removeAtIndex:(NSInteger)index {
NSObject *obj = _mArray->removeIndex((int)index);
return obj;
}
- (int)size {
return _mArray->size();
}
- (void)dealloc{
delete _mArray;
}
- (NSString *)description {
int count = _mArray->size();
NSMutableString *mStr = [[NSMutableString alloc]init];
[mStr appendString:@"[\n"];
for (int i = 0; i < count; i++) {
NSObject *element = _mArray->elmentAtIndex(i);
NSString *str = [element description];
[mStr appendString:str];
if (i != count - 1) {
[mStr appendString:@",\n"];
}
}
[mStr appendString:@"]\n"];
return mStr;
}
@end