说你有名单
NTlist (这里抽象地表示):
["string2", RHSlist ]<-->["string1", RHSlist ]<-->["string3", RHSlist ]
现在,您搜索包含&#34; string1&#34;的NTlist
元素。使用列表迭代器, NTlocation 。搜索结果为NTlocation
然后&#34;点&#34;到中间的元素。
目标是添加到此中间元素的RHS列表中。我这样做:
NTlocation->RHSlist.push_back(RHSentry); //where RHSentry is of type RHSnode.
问题:
为什么中间元素的RHS列表没有更新?相反,迭代器NTlocation具有更新的RHS列表。
感谢您的时间。
#include <list>
#include <iostream>
#include <string.h>
using namespace std;
typedef struct RHSnode {
string token;
} RHSnode;
typedef struct NTnode {
string token;
list<RHSnode> RHSlist;
} NTnode;
list<NTnode>::iterator searchNTList(string NTstring, list<NTnode> &NTlist);
int main(int argc, const char * argv[]) {
list<NTnode> NTlist;
list<NTnode>::iterator NTlocation;
// making three entries in NTList
NTnode entry1;
entry1.token = "string2";
NTlist.push_back(entry1);
NTnode entry2;
entry2.token = "string1";
NTlist.push_back(entry2);
NTnode entry3;
entry3.token = "string3";
NTlist.push_back(entry3);
RHSnode RHSentry;
RHSentry.token = "RHSentry";
//Now, say, I want to put this RHSentry (above) into NTlist with
// element that has it's token as "string1"
// so I search NTlist for token with "string1"
// and remember the NTlist element location with iterator NTlocation
NTlocation = searchNTList("string1", NTlist);
// Now here's where the problem occurs
NTlocation->RHSlist.push_back(RHSentry);
return 0;
}
list<NTnode>::iterator searchNTList(string NTstring, list<NTnode> &NTlist){
//"past-the-end" indications search failure
list<NTnode>::iterator NTlocation = NTlist.end();
list<NTnode>::iterator iterator = NTlist.begin();
for ( ; iterator != NTlist.end() ; iterator++ ){
if (iterator->token.compare(NTstring) == 0){
NTlocation = iterator;
break;
}
}
return NTlocation;
}