通过函数后从列表中删除项目时出错


当我

尝试从我正在处理的程序的列表中删除元素时,

我很难理解发生了什么。
#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;
}

我已经知道导致错误的原因,尝试像这样调用"擦除"功能: 它 = 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

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

相关内容

  • 没有找到相关文章

最新更新