【发布时间】:2013-02-18 23:33:55
【问题描述】:
我是 C++ 的新手,对这个指针和东西感到头疼!
我需要遍历链表结构的列表,读取结构的数据并弹出该条目!
这是我的结构:
struct node {
map<string,double> candidates;
double pathCost;
string source;
node *next; // the reference to the next node
};
通过阅读this 帖子,我创建了我的列表,如下所示:
list<node*> nodeKeeper;
然后初始化第一个值:
node *head;
head= new node;
head->pathCost = 0.0;
head->source="head";
head->next = NULL;
细填充列表和结构:
for(unsigned int i = 0; i < sourceSentence.size(); i++){
node *newNode= new node; //create a temporary node
//DO STUFF HERE
//push currunt node to stack
nodeKeeper.push_back(newNode);
head = newNode;
}
现在我有了结构列表,我想遍历它并弹出元素:
for (list<node*>::const_iterator it=nodeKeeper.begin();it!=nodeKeeper.end();it++){
it->pop_front();
}
这给了我这个错误:
错误:在 '* 中请求成员 'pop_front' it.std::_List_const_iterator<_tp>::operator->()',它是 指针类型 'node* const' (也许你的意思是使用 '->' ?) make: *** [main3.o] 错误 1
看起来我的迭代器指向列表内部,而不是列表本身!
你能告诉我这里出了什么问题吗?!
【问题讨论】:
-
如果允许使用 STL,为什么不使用
list或lists? -
@bilz 是的,我也试过了!我说我是 C++ 的新手,我只是在尝试所有阅读的内容
-
@AndyProwl 这有什么帮助?我需要指向下一个节点的指针!
-
@Moj
list是一个双向链表结构。每个元素都有一个指向下一个和前一个节点的指针。这就是你迭代它的方式。 -
@Moj:这就是
list为您所做的。我会从node中删除next成员,我会使用list<list<node>>(或者可能是vector<list<node>>,除非你有理由选择list)
标签: c++ struct linked-list