2017-03-01 84 views
0

我试图从playerRoster向量中删除Team Roster元素,给出用户定义的球衣号码。我遇到的问题是.erase功能不起作用。结构向量的擦除元素

struct TeamRoster { 
    int jerseyNumber; 
    int playerRating; 
}; 

int numPlayers = 5; 
int searchValue = 0; 
vector<TeamRoster> playerRoster(numPlayers); 
TeamRoster newPlayer; 

unsigned int i = 0; 

cout << "Enter a jersey number: "; 
cin >> searchValue; 
cout << endl; 
for (i = 0; i < playerRoster.size(); ++i) { 
    if (playerRoster.at(i).jerseyNumber == searchValue) { 
     playerRoster.erase(i); 
    } 
} 

我得到这个错误。

main.cpp: In function 'int main()': 
main.cpp:68:36: error: no matching function for call to 'std::vector<main()::TeamRoster>::erase(unsigned int&)' 
      playerRoster.erase(i); 
           ^
In file included from /usr/include/c++/6/vector:64:0, 
      from main.cpp:2: 
/usr/include/c++/6/bits/stl_vector.h:1147:7: note: candidate: std::vector<_Tp, _Alloc>::iterator std::vector<_Tp, _Alloc>::erase(std::vector<_Tp, _Alloc>::const_iterator) [with _Tp = main()::TeamRoster; _Alloc = std::allocator<main()::TeamRoster>; std::vector<_Tp, _Alloc>::iterator = __gnu_cxx::__normal_iterator<main()::TeamRoster*, std::vector<main()::TeamRoster> >; typename std::_Vector_base<_Tp, _Alloc>::pointer = main()::TeamRoster*; std::vector<_Tp, _Alloc>::const_iterator = __gnu_cxx::__normal_iterator<const main()::TeamRoster*, std::vector<main()::TeamRoster> >; typename __gnu_cxx::__alloc_traits<typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type>::const_pointer = const main()::TeamRoster*] 
    erase(const_iterator __position) 
    ^~~~~ 
/usr/include/c++/6/bits/stl_vector.h:1147:7: note: no known conversion for argument 1 from 'unsigned int' to 'std::vector<main()::TeamRoster>::const_iterator {aka __gnu_cxx::__normal_iterator<const main()::TeamRoster*, std::vector<main()::TeamRoster> >}' 
/usr/include/c++/6/bits/stl_vector.h:1174:7: note: candidate: std::vector<_Tp, _Alloc>::iterator std::vector<_Tp, _Alloc>::erase(std::vector<_Tp, _Alloc>::const_iterator, std::vector<_Tp, _Alloc>::const_iterator) [with _Tp = main()::TeamRoster; _Alloc = std::allocator<main()::TeamRoster>; std::vector<_Tp, _Alloc>::iterator = __gnu_cxx::__normal_iterator<main()::TeamRoster*, std::vector<main()::TeamRoster> >; typename std::_Vector_base<_Tp, _Alloc>::pointer = main()::TeamRoster*; std::vector<_Tp, _Alloc>::const_iterator = __gnu_cxx::__normal_iterator<const main()::TeamRoster*, std::vector<main()::TeamRoster> >; typename __gnu_cxx::__alloc_traits<typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type>::const_pointer = const  main()::TeamRoster*] 
    erase(const_iterator __first, const_iterator __last) 
    ^~~~~ 
/usr/include/c++/6/bits/stl_vector.h:1174:7: note: candidate expects 2 arguments, 1 provided 

我看着cplusplus.com,发现C++ 94和C++ 11 iterator erase (const_iterator position);(11)之间iterator erase (iterator position);(94)的差。从我得到的错误,它看起来像我的编译器正在使用C++ 11。

const_iterator是否意味着我必须使用一个常量与.erase()函数,如果有,是否有不同的函数可以在这种情况下工作?

如果这不是问题,你能帮我弄清楚是什么?

+3

'擦除()'需要一个* *迭代器,而不是一个无符号'int'。 – Biffen

+0

我还在学习C++,我应该去学习一下迭代器是什么以及如何使用它,或者最好是找出一种不同的方法从结构向量中移除一个元素? –

+0

迭代器在现代C++中使用很多,所以最好在它们上面进行读取。 – Biffen

回答

0

如果只有一个要素,你要删除,你可以试试下面的代码:

for(auto iter = playerRoster.begin(); iter != playerRoster.end(); ++iter){ 
    if(iter->jerseyNumber == searchValue){ 
     iter = playerRoster.erase(iter); 
     break; 
    } 
} 

或者,如果你想删除所有匹配的元素,你可以尝试:

for(auto iter = playerRoster.begin(); iter != playerRoster.end();){ 
    if(iter->jerseyNumber == searchValue){ 
     iter = playerRoster.erase(iter); 
    }else{ 
     ++iter; 
    } 
} 

记住擦除返回被擦除之后的元素。

示例代码可以发现:http://www.cplusplus.com/reference/vector/vector/erase/

+0

在示例代码中,他们有'.erase(myvector.begin()+ 5)'。 '.erase(playerRoster.bigin()+ i)'工作吗? –

+0

@JacobBischoff哦,是的,它的工作,我想。通常我不会那样写代码。 – everettjf