This is follow up of my last question. I have tried to make this example simple. I have class Hold
which contains a vector of int and class CollectHold
which containts the vector of collection of class Hold
I have two methods in class CollectHold
, void Insert(size_t index, value)
and std::vector<int> GetVec()
. Every time Insert
method is called it has to determine which of the collection of vector of class Hold
it has to insert. This for me looks like really bad design. Also for GetVec
I have to append all the vector on the fly every time this function is called and looks really memory intensive.
Do you think the API design here is appropriate? By Appropriate I mean the naming convention, optimization for GetVec
memory wise and fast indexing for Insert
method of class CollectHold
I was also thinking of of costume iterator (iterator design pattern) inside class CollectHold
.
#include <vector> class Hold{ public: hold(size_t n):vec(n,0){} void Insert(size_t index, value){ vec.at(index) = value; } std::vector<int> GetVec(){ return vec; } private: std::vector<int> vec; } // lets assume during initialization they are initialized by zero //holder[0] = [0][0][0][0] //holder[1] = [0][0][0] //holder[2] = [0][0][0][0][0] class CollectHold{ public: void AddHold(size_t size) { Hold tmp(size); holder.push_back(tmp); } void Insert(size_t index, value){ // determine while of the holder's vector this index // belongs to then // holder[1].at(1) = value } std::vector<int> GetVec(){ // append holder[0] holder[1] holder[2] on the fly and return } private: std::vector<Hold> holder; }; int main(){ CollectHold collect_holder; collect_holder.AddHold(4); collect_holder.AddHold(3); collect_holder.AddHold(5); collect_holder.insert(6, 7); std::vector<int> get_all_val = collect_holder.GetVec(); }