0

I'm trying to create an array which holds a pointer to a container at each element. The container is of type Queue and is basically a linked list queue. I want to add an item to the linked list at a given index but I can't figure out how to do it.

This is the array being initialized:

Queue<string> * table;

table = new Queue<string>[tableSize];

This is what I want (I know it doesn't work):

table[5] = "SomeString";

I've tried:

Queue<string> *ptr = table[5];
ptr->insert(SomeString);

1 Answers1

3

I'm trying to create an array which holds a pointer to a container at each element.

For a local array:

Queue<string> *table[tableSize];

For a heap-allocated array:

Queue<string> ** table;
table = new Queue<string>*[tableSize];

Use either one the same:

table[5]->insert("Some String");

But, I advise you to ditch the naked pointers and the manual allocation. Use std::vector<Queue<string>>:

std::vector<Queue<string>> v(tablesize);
v[5].insert("Some String");
Robᵩ
  • 163,533
  • 20
  • 239
  • 308