我試圖從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()
函數,如果有,是否有不同的函數可以在這種情況下工作?
如果這不是問題,你能幫我弄清楚是什麼?
'擦除()'需要一個* *迭代器,而不是一個無符號'int'。 – Biffen
我還在學習C++,我應該去學習一下迭代器是什麼以及如何使用它,或者最好是找出一種不同的方法從結構向量中移除一個元素? –
迭代器在現代C++中使用很多,所以最好在它們上面進行讀取。 – Biffen