What is the most efficient way to remove alternate (odd indexed or even indexed) elements in an List
without using a place holder list variable?
A
I would use the standard pattern generally used for STL containers. Do a remove followed by an erase.
This way you will not confuse people that are used to seeing this pattern.
template
struct RemoveEven
{
RemoveEven():count(0) {}
bool operator()(T const&)
{
bool result = count%2 == 0;
count++;
return result;
}
private:
std::size_t count;
};
int main()
{
std::list a;
a.erase(std::remove_if(a.begin(),a.end(),RemoveEven()),a.end());
}