Remove elements from a list
I have a vector<float> source vector and a vector<int> keys. I would like to remove all element in the keys from the source vector. i.e. if the keys array is <1, 3, 4, 5> I would like to remove 1st, 3rd , 4th, 5th element from the source vector. how can I do that? Thank you.
This is most likely not the best solution but it does the work. Please see the comment how the keys should be organized (sorted and no doubles). The algorithm will erase values starting with the biggest keys. typedef std::vector<float> tFloatVector; tFloatVector aFloatVec; void erase( int nKey ) { if( nKey < aFloatVec.size() ) { tFloatVector::iterator iFloatVec = aFloatVec.begin(); iFloatVec += nKey - 1; if( iFloatVec != aFloatVec.end() ) aFloatVec.erase( iFloatVec ); } } int _tmain(int argc, _TCHAR* argv[]) { typedef std::vector<int> tIntVector; // assuming int's are sorted small to big // and there are no doubles. tIntVector aIntVec; aIntVec.push_back( 1 ); aIntVec.push_back( 3 ); aIntVec.push_back( 4 ); aIntVec.push_back( 5 ); aFloatVec.push_back( 1.0F ); aFloatVec.push_back( 2.0F ); aFloatVec.push_back( 3.0F ); aFloatVec.push_back( 4.0F ); aFloatVec.push_back( 5.0F ); aFloatVec.push_back( 6.0F ); aFloatVec.push_back( 7.0F ); aFloatVec.push_back( 8.0F ); std::reverse( aIntVec.begin(), aIntVec.end() ); std::for_each( aIntVec.begin(), aIntVec.end(), boost::bind( &erase, _1 ) ); } Would be interesting to see how the boost expert would solve the problem. ;-) Christian
Meryl Silverburgh wrote:
I have a vector<float> source vector and a vector<int> keys.
I would like to remove all element in the keys from the source vector.
i.e. if the keys array is <1, 3, 4, 5> I would like to remove 1st, 3rd , 4th, 5th element from the source vector.
how can I do that?
Thank you.
I think this is as simple as possible: sort(keys.begin(), keys.end(), greater<int>()); // maybe use std::unique to remove duplicates BOOST_FOREACH(int key, keys) { assert(key >= 0); assert(key < data.size()); data.erase(boost::next(data.begin(), key)); }
Pablo Aguilar wrote:
Meryl Silverburgh wrote:
I think this is as simple as possible:
sort(keys.begin(), keys.end(), greater<int>()); // maybe use std::unique to remove duplicates BOOST_FOREACH(int key, keys) { assert(key >= 0); assert(key < data.size()); data.erase(boost::next(data.begin(), key)); }
I think it's also wrong. You need to walk the index list backwards because otherwise you change the indices of the data vector and remove the wrong elements. Sebastian Redl
Sebastian Redl wrote:
Pablo Aguilar wrote:
Meryl Silverburgh wrote:
I think this is as simple as possible:
sort(keys.begin(), keys.end(), greater<int>()); // maybe use std::unique to remove duplicates BOOST_FOREACH(int key, keys) { assert(key >= 0); assert(key < data.size()); data.erase(boost::next(data.begin(), key)); }
I think it's also wrong. You need to walk the index list backwards because otherwise you change the indices of the data vector and remove the wrong elements.
Sebastian Redl
Hmm... I think you missed the sort call, just before FOREACH. Pablo Aguilar
participants (4)
-
Christian Henning
-
Meryl Silverburgh
-
Pablo Aguilar
-
Sebastian Redl