我正在尝试创建一个数组,该数组在每个元素处保存一个指向容器的指针。该容器是 Queue 类型,基本上是一个链表队列。我想在给定索引处向链表添加一个项目,但我不知道该怎么做。
这是正在初始化的数组:
Queue<string> * table;
table = new Queue<string>[tableSize];
这就是我想要的(我知道这行不通):
table[5] = "SomeString";
我试过:
Queue<string> *ptr = table[5];
ptr->insert(SomeString);
最佳答案
I'm trying to create an array which holds a pointer to a container at each element.
对于本地数组:
Queue<string> *table[tableSize];
对于堆分配的数组:
Queue<string> ** table;
table = new Queue<string>*[tableSize];
使用其中任何一个:
table[5]->insert("Some String");
但是,我建议您放弃裸指针和手动分配。使用 std::vector<Queue<string>>
:
std::vector<Queue<string>> v(tablesize);
v[5].insert("Some String");
关于c++ - 给定一个指向容器的指针,我如何向它添加一个项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10378079/