【问题标题】:C++ list iterator not accessing content of list [closed]C ++列表迭代器不访问列表的内容[关闭]
【发布时间】:2016-09-17 13:36:15
【问题描述】:

可视化
NTlist:[ NTstring, RHSlist ][ NTstring, RHSlist ][ NTstring, RHSlist ]...

Ntlocation “指向”例如中间链接 ^(上图),因为这里的 NTstring 与我正在搜索的内容相匹配。

我想做什么:
使用 NTlocation “指针”我想在此处访问和修改 RHSlist。

问题:
Debugger shows RHSlist data being put into NTlocation

But I want RHSlist data to be in element of NTlist

非常感谢您的帮助!

编辑。每个请求添加代码。再次感谢!

typedef struct RHSnode {
  token_type type;
  string token;
} RHSnode;

typedef struct NTnode {
  token_type type;
  string token;
  list<RHSnode> RHSlist;
} NTnode;

int function {
  list<NTnode> NTlist;
  list<NTnode>::iterator NTlocation;

  freopen("example.txt", "r", stdin);
  t_type = getToken(); //this function gets tokens from txt file above
  // code has been simplified for your viewing pleasure
  // but basically NT.list is created for each token listed at start of 
  // text file in the following manner.  (This works fine)
  NTnode entry;
  entry.token = string(current_token);
  NTlist.push_back(entry);
  t_type = getToken();

  // loop until all tokens are read 
  {
    t_type = getToken(); 
    string NTstring = string(current_token);
    NTlocation = searchNTList(NTstring, NTlist);

    RHSnode entry;
    entry.token = string(current_token);

    //****Here is the problem:
    NTlocation->RHSlist.push_back(entry);            

  }

  return 1;
}

list<NTnode>::iterator searchNTList(string NTstring, list<NTnode> &NTlist){
  list<NTnode>::iterator NTlocation = NTlist.end(); //"past-the-end"     indications search failure
  list<NTnode>::iterator iterator = NTlist.begin();

  for ( ; iterator != NTlist.end() ; iterator++ ){

    if (iterator->token.compare(NTstring) == 0){
      NTlocation = iterator;
      break; 
    }

  }
  return NTlocation;
}

【问题讨论】:

  • 显示代码,散文都是徒劳的!
  • 您的问题中还有大量代码。您的问题是否需要所有这些?你能把它减少到仍然重现你的问题的最小数量吗?
  • @BillLynch 感谢您查看代码。它已被简化。非常尊重您,先生。
  • 还有@πάνταῥεῖ,我添加了简化的代码和调试器图像。如果还有什么可以帮助您的,请告诉我

标签: c++ list stl iterator stdlist


【解决方案1】:

至少:

list<NTnode>::iterator searchNTList(string NTstring, list<NTnode> NTlist){

这会将迭代器返回到NTlist 的本地副本。您可能希望通过 const 引用传递 NTlist

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-11
    • 1970-01-01
    • 2021-02-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多