简体   繁体   中英

Erase element of struct vector

I am trying to delete a Team Roster element from the playerRoster vector given a user defined jersey number. The problem I am running into is the .erase function isn't working.

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);
   }
}

I am getting this error.

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

I looked on cplusplus.com and noticed a difference between c++94 and c++11 iterator erase (const_iterator position); (11), iterator erase (iterator position); (94). From the error I'm getting, it looks like my compiler is using c++11.

Does const_iterator mean that I have to use a constant with the .erase() function, and if so, is there a different function that will work in this case?

If this isn't the problem, can you help me figure out what is?

If only one element you want to erase, you could try code below:

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

Or if you want to erase all element that match, you could try :

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

Remember erase return the element after the one that be erased.

Sample code can be found : http://www.cplusplus.com/reference/vector/vector/erase/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM