0

How do I make an array of pointers? I'm trying to make an array of pointers to a class in order to provide both O(1) access and have O(1) insertion. This is essentially what I've got. It has to be an array of pointers hence datalist being a pointer to a pointer. I think this is more of a lack of syntactical knowledge.

class Data{

};

class List{

private:

    Data** datalist;

public:
    explicit List(int s = 20):datalist{/* new something */}{}

};

Thanks.

anonrose
  • 1
  • 4
  • 2
    What's your question? – Ryan Bemrose May 30 '15 at 05:50
  • How do I make an array of pointers? – anonrose May 30 '15 at 05:56
  • To elaborate a bit further on that, are you asking 1) if your assumptions about the big-O notation are good? 2) if the design of your code is good? 3) if your C++ code is syntactically correct? or 4) if your C++ code is the best way to make the data structure you're describing? If this is for more than just an exercise, you might want to consider the use of std::vector or explain why that's not what you want; that's probably what I would choose here. (*sigh* crossed timing with the above comment) – J Trana May 30 '15 at 05:56
  • Recommend close as dup of this one: http://stackoverflow.com/questions/620843/how-do-i-create-an-array-of-pointers – J Trana May 30 '15 at 05:58
  • A list of lists will not give you O(1) get and put. You need a hashmap for this. – Bartlomiej Lewandowski May 30 '15 at 05:59
  • Yea that's exactly what I should do thanks. – anonrose May 30 '15 at 06:01

1 Answers1

0

To make an array of pointers, you can do it in this way.

class Data{

};

class List{

private:

    Data** datalist;

public:
    explicit List(int s = 20):datalist(new Data*[s]) {}

};
Xiaotian Pei
  • 3,210
  • 21
  • 40