简体   繁体   English

使用C ++ std :: list iterator替换列表中的项目

[英]Replacing items in a list using a C++ std::list iterator

The basic structure of my code is: 我的代码的基本结构是:

using namespace std;
void recursiveFunction(list <int> &jobs,...){
list<int>::iterator i;
int ii;
//code missing
    for(i=jobs.begin(); i != jobs.end(); ++i){
        //more code missing
        list<int>::iterator tempi(i);
        ii=*i;
        jobs.erase(tempi);
        recursiveFunction(jobs,...);
        jobs.insert(i,ii);
    }
}

As I have discovered, any pointer pointing to a position that is erased is invalidated, so i is invalidated. 正如我所发现的,任何指向被擦除位置的指针都是无效的,因此我无效。 Is there some way to reinsert the job numbers in this way? 有没有办法以这种方式重新插入工作号码? Without the performance hit of creating a new list each recursion? 没有创建每个递归的新列表的性能?

Is there a way to use something other than a list iterator, perhaps? 有没有办法使用除列表迭代器之外的东西?

list::erase returns an iterator to the element past the (last) erased element, and since list::insert will insert before the element whose iterator you pass it, that's perfect for your needs: list::erase返回超过(last)擦除元素的元素的迭代器,并且因为list::insert将在您传递iterator的元素之前插入,这非常适合您的需求:

using namespace std;
void recursiveFunction(list <int> &jobs,...){
  //...
  for(auto i = begin(jobs); i != end(jobs);){ 
    //...
    auto tmpElem = *i;
    i = jobs.erase(i);
    recursiveFunction(jobs,...);
    jobs.insert(i,tmpElem);
  }
}

Notes: 笔记:

  • with i=jobs.erase(i) you effectively increment i . i=jobs.erase(i)你有效地增加i So leave the increment in the for loop. 所以在for循环中保留增量。 Alternatively use i=jobs.insert(i,tmpElem) later, so i points to the same element again 或者稍后使用i=jobs.insert(i,tmpElem) ,所以i再次指向相同的元素
  • Declare variables (eg i , tmpElem ) as local as possible, as a matter of good style and maintainability 作为良好的风格和可维护性,将变量(例如itmpElem )声明为尽可能本地
  • Give variables meaningful names 给变量有意义的名称

Depending on what the function does there might be other possibilities to achieve what you want. 根据功能的不同,可能还有其他可能实现您想要的功能。 This way you will work on each subset of list elements, and on many of those multiple times. 这样,您将处理列表元素的每个子集,并且多次处理其中的许多元素。 Consider the list to have the content {1,2,3} , heres' what is going to happen (in pseudo-code): 考虑列表以获得内容{1,2,3} ,继承人将会发生什么(在伪代码中):

recursiveFunction({1,2,3},...)
  for-loop, i = &1
    erase(1)
    recursiveFunction({2,3},...)
      for-loop, i = &2
        erase(2)
        recursiveFunction({3},...)    //a
        insert(2)
      //...
    insert(1)
  for-looop, i = &2
    erase(2)
    recursiveFunction({1,3},...)
      for-loop, i = &1
        erase(1)
        recursiveFunction({3},...)    //b
        insert(1)
      //...
    insert(2)
  //...

Lines a and b look the same, although the additional parameters might not be the same - I can't tell from your code. 线a和b看起来一样,虽然附加参数可能不一样 - 我无法从你的代码中看出来。 So just keep it in mind and consider if this is what you actually want. 所以请记住这一点,并考虑这是否是您真正想要的。

Recursion does not seem to be the right tool for your problem. 递归似乎不是您的问题的正确工具。 If you have a list containing { 1, 2, 3, 4 } and all three qualify, it looks like this is what would happen: 如果你有一个包含{1,2,3,4}的列表并且所有三个都符合条件,那么看起来会发生这种情况:

recurse({1,2,3,4});
  it.1 = (1)
  remove 1
  recurse({2,3,4});
    it.2 = (2)
    remove 2
    recurse({3,4});
      it.3 = (3)
      remove 3
      recurse({4});
        it.4 = (4)
        remove 4
        recurse({})
        reinsert 4 ({4})
      reinsert 3 ({3,4})
      it.3 = (4)
      remove 4
      recurse({3});
        it.4 = (3)
        remove 3
        recurse({})
        reinsert 3 ({3})
      reinsert 4 ({3,4})
    reinsert 2 ({2,3,4})
    it.2 = (3)
    remove 3 leaving ({2,4})
... etc ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM