遍历链表的列表 我对C++有点新的看法,用这个指针和东西都会头疼!
我需要遍历链表中的struct列表,读取struct的数据并弹出该条目 这是我的结构:遍历链表的列表 我对C++有点新的看法,用这个指针和东西都会头疼!,c++,struct,linked-list,C++,Struct,Linked List,我需要遍历链表中的struct列表,读取struct的数据并弹出该条目 这是我的结构: struct node { map<string,double> candidates; double pathCost; string source; node *next; // the reference to the next node }; 精简填充列表和结构: for(unsigned int i = 0; i < sou
struct node {
map<string,double> candidates;
double pathCost;
string source;
node *next; // the reference to the next node
};
精简填充列表和结构:
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(unsigned int i=0;i
现在我有了struct的列表,我想遍历它并弹出元素:
for (list<node*>::const_iterator it=nodeKeeper.begin();it!=nodeKeeper.end();it++){
it->pop_front();
}
for(list::const_迭代器it=nodeKeeper.begin();it!=nodeKeeper.end();it++){
it->pop_front();
}
这给了我一个错误:
错误:请求中的成员“pop_front”*
it.std::_List_const_iterator::operator->()',它是
指针类型“node*const”(可能您想使用“->”?)使:***
[main 3.o]错误1
看起来我的迭代器指向列表内部,而不是列表本身
你能告诉我这里怎么了吗 如果只需删除元素,请使用:
要读取元素的内容,然后删除,请尝试以下操作:
for (std::list<node*>::const_iterator it = nodeKeeper.begin(); it != nodeKeeper.end(); ++it) {
std::cout << (*it)->source;
// do more reading
nodeKeeper.pop_front();
}
for(std::list::const_迭代器it=nodeKeeper.begin();it!=nodeKeeper.end();++it){
std::cout源;
//多读书
nodeKeeper.pop_front();
}
或使用C++11:
for (const auto& a : nodeKeeper) {
std::cout << a->source;
nodeKeeper.pop_front();
}
for(常数自动&a:nodeKeeper){
std::cout源;
nodeKeeper.pop_front();
}
如果您只需移除元件,请使用:
要读取元素的内容,然后删除,请尝试以下操作:
for (std::list<node*>::const_iterator it = nodeKeeper.begin(); it != nodeKeeper.end(); ++it) {
std::cout << (*it)->source;
// do more reading
nodeKeeper.pop_front();
}
for(std::list::const_迭代器it=nodeKeeper.begin();it!=nodeKeeper.end();++it){
std::cout源;
//多读书
nodeKeeper.pop_front();
}
或使用C++11:
for (const auto& a : nodeKeeper) {
std::cout << a->source;
nodeKeeper.pop_front();
}
for(常数自动&a:nodeKeeper){
std::cout源;
nodeKeeper.pop_front();
}
如果您的目标是拥有节点结构的单个列表,则无需自行管理下一个指针。插入将保持不变(减去头=
行)
要弹出列表中的所有元素,请执行以下操作
int sizeOfList = nodeKeeper.size();
for( int i =0; i < sizeOfList; i++) {
//if you want to do something with the last element
node * temp = nodeKeeper.back();
//do stuff with that node
//done with the node free the memory
delete temp;
nodeKeeper.pop_back();
}
int-sizeOfList=nodeKeeper.size();
对于(int i=0;i
编译/运行这里的示例:如果您的目标是拥有节点结构的单个列表,则无需自行管理下一个指针。插入将保持不变(减去
头=
行)
要弹出列表中的所有元素,请执行以下操作
int sizeOfList = nodeKeeper.size();
for( int i =0; i < sizeOfList; i++) {
//if you want to do something with the last element
node * temp = nodeKeeper.back();
//do stuff with that node
//done with the node free the memory
delete temp;
nodeKeeper.pop_back();
}
int-sizeOfList=nodeKeeper.size();
对于(int i=0;i
这里的编译/运行示例:如果允许使用STL,为什么不使用
列表的列表呢?@bilz是的,我也试过了!我说我是C++新手,我只是尝试一切read@AndyProwl这有什么帮助?我需要有指向下一个节点的指针@Mojlist
是一种双链表结构。每个元素都有一个指向下一个和上一个节点的指针。这就是你迭代它的方式。@Moj:list
就是这样为你做的。我会从节点
中删除下一个成员,我会使用列表
(或者可能是向量
,除非你有理由选择列表
),如果你被允许使用STL,为什么不使用列表
!我说我是C++新手,我只是尝试一切read@AndyProwl这有什么帮助?我需要有指向下一个节点的指针@Mojlist
是一种双链表结构。每个元素都有一个指向下一个和上一个节点的指针。这就是你迭代它的方式。@Moj:list
就是这样为你做的。我将从节点
中删除下一个
成员,我将使用列表
(或者可能是向量
,除非您有理由选择列表
)@juanchopanza不知道该方法。我将在回答中使用它。如何访问每个元素的数据?类似于:nodeKeeper.front()->source;因为现在它只打印出最后一个元素“head”@Moj,我正在更新我的答案。等等plz@Moj顺便说一句,它应该是(*it)->source
和nodeKeeper.pop_front()
@Moj我的更新现在有帮助吗?我用coutsource
读取数据,然后用nodeKeeper.pop\u front()
弹出数据。这对你有用吗?@juanchopanza不知道这种方法。我将在回答中使用它。如何访问每个元素的数据?类似于:nodeKeeper.front()->source;因为现在它只打印出最后一个元素“head”@Moj,我正在更新我的答案。等等plz@Moj顺便说一句,它应该是(*it)->source
和nodeKeeper.pop_front()
@Moj我的更新现在有帮助吗?我用coutsource
读取数据,然后用nodeKeeper.pop\u front()
弹出数据。这对你有用吗?谢谢你,罗伯特。帮了很多忙,汉克斯·罗伯特。帮了大忙