Closed. This question is off-topic。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
                        
                        3年前关闭。
                                                                                            
                
        
可视化:
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;
}

最佳答案

至少:

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


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

10-08 01:05