These two lines can be simplified to: ipf->set_pfIndex( std::distance(ipfbegin,ipf) );