我正在尝试从给定用户定义的球衣号码的Team Roster
向量中删除playerRoster
元素。我遇到的问题是.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()
函数的常量,如果是,那么在这种情况下是否有一个不同的函数?
如果这不是问题,你能帮我搞清楚是什么吗?
答案 0 :(得分: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/