gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 02:53:42 25 4
gpt4 key购买 nike

我决定有一天用 c++ 创建一个类,其存储功能类似于 objective-c 中的 NSMutableArray(我知道 vector 是这类事情的 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;
}

原来如此。任何帮助将不胜感激。

最佳答案

这将回答你的问题

class template

这是我如何使用模板实现 vector 类的一部分的示例

这是一个文件vector.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>你将有字符 vector 。注意:头文件永远不会被编译

关于c++ 各种数据类型的可变数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17715810/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com