I am iterating over a vector of boost::tuples in order to find an element. However I would also like to find the exact position of this element in the vector in order to delete it later. This is the code, however the std::distance does not give me correct value.
int Controller::isValid(int Id, int& pos) {
pos = 0;
for( std::vector< boost::tuple<int,std::string, std::string, std::string> >::const_iterator it = games.begin(); it != games.end(); it++) {
if( boost::get<0>(*it) == Id) {
pos = std::distance< std::vector< boost::tuple<int,std::string, std::string, std::string> >::const_iterator >( games.begin(), it ) ;
return 0;
}
}
For example for a vector with size equals to 5, the std::distance is 8!
Why? Where is the bug in my code?
As Quentin wrote in the comments, an
std::vectorofboost::tuples can be searched for withstd::find_if, just like any other type.Note that
std::vector::eraseallows you to erase an element by its iterator.