为什么 C++ std::vector 中没有 pop_front 方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5563952/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:25:37  来源:igfitidea点击:

Why there is no pop_front method in C++ std::vector?

c++vector

提问by Alessandro Jacopson

Why there is no pop_frontmethod in C++ std::vector?

为什么pop_frontC++ 中没有方法std::vector

回答by Lightness Races in Orbit

Because a std::vectorhas no particular feature regarding inserting elements at the front, unlike some other containers. The functionality provided by each container makes sense for that container.

因为std::vector与其他一些容器不同,a没有关于在前面插入元素的特殊功能。每个容器提供的功能对该容器来说都是有意义的

You probably should be using a std::deque, which is explicitly goodat inserting at the front andback.

你也许应该使用std::deque,这是明确于在前面插入背部。

Check this diagramout.

看看这张图

回答by SEGStriker

Simple. Just try:

简单的。你试一试:

vec.erase(vec.begin());

回答by SEGStriker

vector is typically implemented something like this:

vector 通常是这样实现的:

struct 
{
  T* begin; // points to the first T in the vector
  T* end; // points just after the last T in the vector
  int capacity; // how many Ts of memory were allocated
};

"begin" serves double-duty as "pointer to the first T in the vector" and "pointer to all the memory we allocated." therefore it's impossible to "pop" elements off the front of the vector by simply incrementing "begin" - do this and you no longer have a pointer to the memory you need to deallocate. that would leak memory. so a "pop_front" would need to copy all the Ts from the back of the vector to the front of the vector, and that is comparatively slow. so they decided to leave it out of the standard.

“开始”作为“指向向量中第一个 T 的指针”和“指向我们分配的所有内存的指针”有双重作用。因此,不可能通过简单地增加“开始”来“弹出”向量前面的元素 - 这样做并且您不再有指向需要释放的内存的指针。那会泄漏内存。所以“pop_front”需要将所有的 T 从向量的后面复制到向量的前面,这相对较慢。所以他们决定将其排除在标准之外。

what you want is something like this:

你想要的是这样的:

struct 
{
  T* allocated; // points to all the memory we allocated
  T* begin; // points to the first T in the vector
  T* end; // points just after the last T in the vector
  int capacity; // how many Ts of memory were allocated
};

with this, you can "pop_front" by moving "begin" forward and backward with no danger of forgetting which memory to deallocate later. why doesn't std::vector work this way? i guess it was a matter of taste among those who wrote the standard. their goal was probably to provide the simplest possible "dynamically resizeable array" they could, and i think they succeeded.

有了这个,您可以通过向前和向后移动“开始”来“pop_front”,而不会忘记稍后要释放的内存。为什么 std::vector 不这样工作?我想这是编写标准的人的品味问题。他们的目标可能是尽可能提供最简单的“可动态调整大小的数组”,我认为他们成功了。

回答by orlp

Because push_backand pop_backare special operations for a vector that require only O(1)computation. Any other push or pop takes O(n).

因为push_backpop_back是只需要O(1)计算的向量的特殊操作。任何其他 push 或 pop 需要O(n)

This is not a "bug" or a "quirk", this is just a property of the vector container. If you need a fast pop_front consider changing to an other container.

这不是“错误”或“怪癖”,这只是矢量容器的一个属性。如果您需要快速 pop_front,请考虑更改为其他容器。

回答by Roddy

Probably because it would be monumentally slow for large vectors.

可能是因为对于大向量来说它会非常慢。

pop_front()on a vector containing 1000 objects would require 999 operator=()calls.

pop_front()在包含 1000 个对象的向量上需要 999 次operator=()调用。

回答by marchelbling

However, if you need a pop_front and do NOT care about the index of the elementsin the vector, you can do kind ofa pop_front with something like

但是,如果你需要一个pop_front和不关心元素的指数在载体中,你可以做的东西,如一个pop_front

template<typename T>
void pop_front(std::vector<T>& vec)
{
   vec.front() = vec.back();
   vec.pop_back();
}

Dan Higgins talks about this too: https://youtu.be/oBbGC-sUYVA?t=2m52s

Dan Higgins 也谈到了这个:https: //youtu.be/oBbGC-sUYVA?t=2m52s