从向量中删除以添加另一个元素

Remove from vector to add another element

有没有一种方法可以检查一个向量(必须不超过 n 个元素)是否已满并删除第一个元素以添加另一个元素?

你要的是std::deque。 Deque 支持从前后两个方向弹出和推送。 (正如评论中已经提到的)

void my_push(std::deque<int>& sample, int element)
{
    if(sample.size() >= MAX_SIZE)
    {
        sample.pop_front(); // pop off the front element.
        sample.push_front(element);
    }
    else {
         // do whatever you want to...
    }
}