c++ mutable array of various data types? -


i decided 1 day create class in c++ storage capabilities similar of nsmutablearray in objective c (i know vectors goto data type sort of thing made own anyway). made mutablearray class in c++, , far works great. can add , remove objects, insert them specific index if want, without having specify size of array.

so problem is: far, can store objects of type int. there way can make holds other datatypes without having create whole new class specific type? i'm not interested in being able store objects of different datatypes in same mutablearray, want able specify datatype mutablearray holds.

my header file:

#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 

my cpp file:

#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;     (int *i = start + amount; > 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;     (int *i = start; < 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; } 

so there is. appreciated.

this answer question

class template

here example of how implemented part of vector class using template

this 1 file 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 

this file using vetcor class

#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 @ 2 :"<<*a<<std::endl; std::cout<<"element @ 2 using [] operator :"<<vec[5]<<std::endl;   return 0; } 

so way created vector<int> in main in similar manner writing vector<char> have vector of characters. note: header files never compiled


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -