各种数据类型的c++可变数组

c++ mutable array of various data types?

本文关键字:数组 c++ 数据类型      更新时间:2023-10-16

有一天,我决定在c++中创建一个类,它的存储能力与目标c中的NSMutableArray类似(我知道向量是这类事情的goto数据类型,但我还是自己创建了)。因此,我用c++制作了一个mutableArray类,到目前为止,它运行得很好。我可以添加和删除对象,如果需要,可以将它们插入到特定的索引中,而无需指定数组的大小。

所以我的问题是:到目前为止,它只能存储int类型的对象。有没有什么方法可以让它保存其他数据类型,而不必为特定类型创建一个全新的类?我对能够在同一个mutableArray中存储不同数据类型的对象不感兴趣,我只想能够指定我的mutableArray包含的数据类型。

我的头文件:

#define MUTABLEARRAY_H

class mutableArray
{
    public:
        mutableArray();
        virtual ~mutableArray();
        void initWithSize(int length);
        void initWithArrayThroughIndeces(int nums[], int minimum, int maximum);
        void addObject(int number);
        void insertObjectAtIndex(int number, int index);
        void changeSize(int length);
        void removeLastObject();
        void removeObjectAtIndex(int index);
        int objectAtIndex(int index);
        int lastObject();
        int firstObject();
        int countObjects();
    protected:
    private:
        int *start;
        int amount;
};
#endif // MUTABLEARRAY_H

我的cpp文件:

#include "mutableArray.h"
mutableArray::mutableArray()
{
    //ctor
    start = new int;
    amount = 0;
}
mutableArray::~mutableArray()
{
    //dtor
}
void mutableArray::initWithSize(int length){
    amount = length;
}
void mutableArray::initWithArrayThroughIndeces(int nums[], int minimum, int maximum){
    amount = maximum - minimum;
    start = nums + minimum;
}
void mutableArray::addObject(int number){
    amount++;
    start[amount] = number;
}
void mutableArray::insertObjectAtIndex(int number, int index){
    amount++;
    int j = 0;
    for (int *i = start + amount; i > start; i--){
        if (j >= index){
            start[j + 1] = *i;
        }
        j++;
    }
    start[index] = number;
}
void mutableArray::removeLastObject(){
    amount--;
}
void mutableArray::removeObjectAtIndex(int index){
    amount--;
    int j = 0;
    for (int *i = start; i < start + amount; i++){
        if (j != index){
            start[j] = *i;
            j++;
        }
    }
}
int mutableArray::objectAtIndex(int index){
    return start[index];
}
int mutableArray::lastObject(){
    return start[amount];
}
int mutableArray::firstObject(){
    return *start;
}
int mutableArray::countObjects(){
    return amount;
}

就这样。任何帮助都将不胜感激。

这将回答您的问题

类模板

下面是我如何使用模板实现矢量类的一部分的例子

这是一个文件向量.h

#ifndef VECTOR_H
#define VECTOR_H
#include <iostream>
#include<stdlib.h>
#include<malloc.h>

template <typename T> 
class Vector{
private:
    T *buffer;
    int threshold;
    int length;
    void Allocate();
    void ReAllocate(int);
public:

    Vector();
    ~Vector();
    void push_back (const T& val);
    void pop_back();
    void clear(void);
    void erase (int position);
    void erase (int first, int last);
    int  capacity() const;
    int size() const;
    T* at(int n) const;
    T& operator[] (int n) const;
};
template <typename T> 
Vector<T>:: Vector(){
    buffer=NULL;
    length=0;
    threshold=10;
    Allocate();
}
template <typename T> 
void Vector<T>::Allocate(){
    buffer = (T*)(malloc(threshold*sizeof(T)));
}
template <typename T> 
void Vector<T>::ReAllocate(int size_x){
  std::cout<<"In buffer realloc"<<std::endl;
        threshold=threshold+size_x;
    buffer = (T*)(realloc(buffer,(sizeof(T))*threshold));
}
template <typename T> 
void Vector<T>::push_back (const T& val){
     if(length<threshold){
    buffer[length]=val;
    std::cout<<buffer[length]<<std::endl;
    length++;
    }
   else{
       ReAllocate(10);
       push_back(val);
   }
}
template <typename T> 
void Vector<T>::erase (int first, int last){
    T *tempBuffer=buffer;
    if(first>=0&&last<length){
                int count=0;
            for(int i=0;i<length;i++){
                if(i<first||i>last){
                    buffer[count]=buffer[i];
                count++;
                }
        }
        length=count;
    }else{
            // illegal params
              }
}
template <typename T> 
void Vector<T>::erase(int position){

if(position>=0&&position<length){
                int count=0;
            for(int i=0;i<length;i++){
                if(i!=position-1){
                    buffer[count]=buffer[i];
                count++;
                }
        }
        length--;
    }else{
            // illegal params
              }
}

template <typename T> 
Vector<T>:: ~Vector(){
    free(buffer);
    length=0;
    threshold=10;
    Allocate();
}
template <typename T> 
int Vector<T>::capacity() const{
    return threshold;
}
template <typename T> 
int Vector<T>::size() const{
    return length;
}

template <typename T> 
T* Vector<T>::at(int n) const{
    if(n>0&&n<length){
        return &buffer[n];
    }
    else return NULL;
}

template<typename T>
void Vector<T>::clear(void){
    buffer[length]=0;
    length=0;
}

template<typename T>
T& Vector<T>::operator[](int n) const{
if(n>0&&n<length){
return buffer[n];
}
}

#endif

这是另一个使用我的vetcor类的文件

#include"Vector.h"
#include<iostream>

int main(){

Vector<int> vec;
vec.push_back(2);
vec.push_back(3);
vec.push_back(4);
vec.push_back(5);
vec.push_back(2);
vec.push_back(3);
vec.push_back(4);
vec.push_back(5);
vec.push_back(2);
vec.push_back(3);
vec.push_back(4);
vec.push_back(5);
vec.erase(1);
std::cout<<vec.capacity()<<std::endl;
std::cout<<vec.size()<<std::endl;
int* a=vec.at(2);
std::cout<<"Element At 2 is :"<<*a<<std::endl;
std::cout<<"Element At 2 using [] operator :"<<vec[5]<<std::endl;

return 0;
}

因此,我以类似的方式创建Vector<int>,只需编写Vector<char>,就可以获得字符向量。注意:头文件从不编译