【问题标题】:error deleting item from list after passing through function通过函数后从列表中删除项目时出错
【发布时间】:2015-02-27 08:25:09
【问题描述】:

当我尝试从我正在处理的程序的列表中删除一个元素时,我很难理解发生了什么。

#include <cmath>
#include <cstdio>
#include <list>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int findHigherSkillLevel(int skillLevel, list<int>::iterator *it, list<int> &list) {
  if (it == NULL) return 0;

  if (**it == (skillLevel + 1)) {
    it = list.erase(it);    
    return 1 + findHigherSkillLevel(**it, it, list) + findHigherSkillLevel(**it, --it, list);
  }

  return 0;
}

int findLowerSkillLevel(int skillLevel, list<int>::iterator *it, list<int> &list) {
  if (it == NULL) return 0;

  if (**it == (skillLevel - 1)) {
    it = list.erase(it);
    return 1 + findLowerSkillLevel(**it, it, list) + findLowerSkillLevel(**it, --it, list);
  }

  return 0;
}

int findGroupsSizes(int skillLevel, list<int>::iterator *it, list<int> &list) {
  if (it == NULL) return 0;

  int groupSize = 1;
  it = list.erase(it);
  groupSize += findHigherSkillLevel(**it, it, list) + findLowerSkillLevel(**it, it, list);

  return groupSize;
}

int main() {
  int t; // the number of test cases
  cin >> t;
  vector<list<int> > skillLevels(t, list<int>());
  // input for each test case
  for (int i = 0; i < t; i++) {
    int n; // number of students for this test case
    cin >> n;

    // initialize the list for this test case
    for (int j = 0; j < n; j++) {
       int skillLevel;
       cin >> skillLevel;
       skillLevels[i].push_back(skillLevel);
    }
  }

  // recursively scan lists for smallest teams
  for (int i = 0; i < t; i++) {
    int minGroupNumber = skillLevels[i].size();
    list<int>::iterator iterator = skillLevels[i].begin();
    int skillLevel = skillLevels[i].front();
    while (!skillLevels[i].empty()) {
      int currentGroupSize = findGroupsSizes(skillLevel, &iterator, skillLevels[i]); 
      if (currentGroupSize < minGroupNumber)
        minGroupNumber = currentGroupSize;
      skillLevels[i].pop_front();
    }
    cout << minGroupNumber << endl;
  }

  return 0;
}

我已经知道是什么导致了尝试像这样调用“擦除”函数时出错: it = list.erase(it);

我在代码中用粗体输入。我不明白为什么它给我以下输出,因为据我了解,erase() 只需要一个迭代器来删除列表的位置?:

teamFormation.cpp: In function ‘int findHigherSkillLevel(int, std::list<int>::iterator*, std::list<int>&)’:
teamFormation.cpp:13:24: error: no matching function for call to ‘std::list<int>::erase(std::list<int>::iterator*&)’
     it = *list.erase(it);    
                        ^
teamFormation.cpp:13:24: note: candidates are:
In file included from /usr/include/c++/4.8/list:64:0,
                 from teamFormation.cpp:3:
/usr/include/c++/4.8/bits/list.tcc:108:5: note: std::list<_Tp, _Alloc>::iterator std::list<_Tp, _Alloc>::erase(std::list<_Tp, _Alloc>::iterator) [with _Tp = int; _Alloc = std::allocator<int>; std::list<_Tp, _Alloc>::iterator = std::_List_iterator<int>]
     list<_Tp, _Alloc>::
     ^
/usr/include/c++/4.8/bits/list.tcc:108:5: note:   no known conversion for argument 1 from ‘std::list<int>::iterator* {aka std::_List_iterator<int>*}’ to ‘std::list<int>::iterator {aka std::_List_iterator<int>}’
In file included from /usr/include/c++/4.8/list:63:0,
                 from teamFormation.cpp:3:
/usr/include/c++/4.8/bits/stl_list.h:1193:7: note: std::list<_Tp, _Alloc>::iterator std::list<_Tp, _Alloc>::erase(std::list<_Tp, _Alloc>::iterator, std::list<_Tp, _Alloc>::iterator) [with _Tp = int; _Alloc = std::allocator<int>; std::list<_Tp, _Alloc>::iterator = std::_List_iterator<int>]
       erase(iterator __first, iterator __last)
       ^
/usr/include/c++/4.8/bits/stl_list.h:1193:7: note:   candidate expects 2 arguments, 1 provided
teamFormation.cpp: In function ‘int findLowerSkillLevel(int, std::list<int>::iterator*, std::list<int>&)’:
teamFormation.cpp:24:24: error: no matching function for call to ‘std::list<int>::erase(std::list<int>::iterator*&)’
     it = *list.erase(it);
                        ^
teamFormation.cpp:24:24: note: candidates are:
In file included from /usr/include/c++/4.8/list:64:0,
                 from teamFormation.cpp:3:
/usr/include/c++/4.8/bits/list.tcc:108:5: note: std::list<_Tp, _Alloc>::iterator std::list<_Tp, _Alloc>::erase(std::list<_Tp, _Alloc>::iterator) [with _Tp = int; _Alloc = std::allocator<int>; std::list<_Tp, _Alloc>::iterator = std::_List_iterator<int>]
     list<_Tp, _Alloc>::
     ^
/usr/include/c++/4.8/bits/list.tcc:108:5: note:   no known conversion for argument 1 from ‘std::list<int>::iterator* {aka std::_List_iterator<int>*}’ to ‘std::list<int>::iterator {aka std::_List_iterator<int>}’
In file included from /usr/include/c++/4.8/list:63:0,
                 from teamFormation.cpp:3:
/usr/include/c++/4.8/bits/stl_list.h:1193:7: note: std::list<_Tp, _Alloc>::iterator std::list<_Tp, _Alloc>::erase(std::list<_Tp, _Alloc>::iterator, std::list<_Tp, _Alloc>::iterator) [with _Tp = int; _Alloc = std::allocator<int>; std::list<_Tp, _Alloc>::iterator = std::_List_iterator<int>]
       erase(iterator __first, iterator __last)
       ^
/usr/include/c++/4.8/bits/stl_list.h:1193:7: note:   candidate expects 2 arguments, 1 provided
teamFormation.cpp: In function ‘int findGroupsSizes(int, std::list<int>::iterator*, std::list<int>&)’:
teamFormation.cpp:35:22: error: no matching function for call to ‘std::list<int>::erase(std::list<int>::iterator*&)’
   it = *list.erase(it);
                      ^
teamFormation.cpp:35:22: note: candidates are:
In file included from /usr/include/c++/4.8/list:64:0,
                 from teamFormation.cpp:3:
/usr/include/c++/4.8/bits/list.tcc:108:5: note: std::list<_Tp, _Alloc>::iterator std::list<_Tp, _Alloc>::erase(std::list<_Tp, _Alloc>::iterator) [with _Tp = int; _Alloc = std::allocator<int>; std::list<_Tp, _Alloc>::iterator = std::_List_iterator<int>]
     list<_Tp, _Alloc>::
     ^
/usr/include/c++/4.8/bits/list.tcc:108:5: note:   no known conversion for argument 1 from ‘std::list<int>::iterator* {aka std::_List_iterator<int>*}’ to ‘std::list<int>::iterator {aka std::_List_iterator<int>}’
In file included from /usr/include/c++/4.8/list:63:0,
                 from teamFormation.cpp:3:
/usr/include/c++/4.8/bits/stl_list.h:1193:7: note: std::list<_Tp, _Alloc>::iterator std::list<_Tp, _Alloc>::erase(std::list<_Tp, _Alloc>::iterator, std::list<_Tp, _Alloc>::iterator) [with _Tp = int; _Alloc = std::allocator<int>; std::list<_Tp, _Alloc>::iterator = std::_List_iterator<int>]
       erase(iterator __first, iterator __last)
       ^
/usr/include/c++/4.8/bits/stl_list.h:1193:7: note:   candidate expects 2 arguments, 1 provided

【问题讨论】:

    标签: c++ pointers parameters linked-list iterator


    【解决方案1】:

    错误消息是不言自明的:此时it 不是std::list::iterator,它实际上是std::list::iterator*。您必须取消引用指针(就像您在上一行所做的那样)。

    【讨论】:

    • 所以我必须适当地取消引用它?
    • 是的,一般来说TypeType*是不同的类型,你必须正确使用它们。在这种情况下,您要么取消引用指针,要么完全丢失指针并使用引用。
    • 好的,现在可以了,我把它 = list.erase(it) 改成 *it = list.erase(*it) 就好了。感谢您的帮助!
    猜你喜欢
    • 2013-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多