Does this data structure make sense? It's essentially a vector of pointers, intended to point to larger objects. In effect, it gives the benefit of a linked list, with an \$O(1)\$ lookup at the cost of maintaining an array of pointers to each element.
What edge cases am I missing, and what's the formal name of this data structure? (What's the standard data structure used in such situations?)
#include<iostream>
#include<vector>
using namespace std;
template <class T>
class pointerArray
{
vector<void*> addresses;
public:
pointerArray(int initialSize=100)
{
addresses.resize(initialSize);
}
~pointerArray()
{
for(unsigned int i=0;i<addresses.size();i++)
{
if(addresses[i]!=0)
{
delete (T*)addresses[i];
}
}
}
T& operator[](unsigned int pos)
{
if(pos >= addresses.capacity())
{
addresses.resize(pos*2);
}
if(addresses[pos] == 0)
{
T* ptr = new T;
addresses[pos] = ptr;
}
return *(T*)(addresses[pos]);
}
};
int main()
{
pointerArray<string> pa(100);
for(int i=0;i<10000;i++)
{
pa[i]="Hello World This Is a Very Long String";
}
for(int i=0;i<10000;i++)
{
cout<<pa[i]<<endl;
}
}