记录一种删除容器最快办法

for (iterator iter = value.begin(); iter != value.end(); )
{
   if (xxx)
      value.erase(iter++);
   else
      iter++;
}

该方法不适用于vector,原因是官网有一句:

Because vectors use an array as their underlying storage, erasing elements in positions other than the vector endcauses the container to relocate all the elements after the segment erased to their new positions. This is generally an inefficient operation compared to the one performed for the same operation by other kinds of sequence containers (such as list or forward_list).

因此要用下面的方法:

for (iterator iter = value.begin(); iter != value.end(); )
{
   if (xxx)
      iter = value.erase(iter);
   else
      iter++;
}

留下只言片语: