为什么标准 C++ 不可以? 容器适配器提供了明确的功能吗?

发布于 2024-07-13 02:55:12 字数 289 浏览 5 评论 0原文

有谁知道为什么 std::queue、std::stack 和 std::priority_queue 不提供 clear() 成员函数? 我必须伪造这样一个:

std::queue<int> q;
// time passes...
q = std::queue<int>();  // equivalent to clear()

IIRC,clear() 是由所有可以充当底层容器的东西提供的。 有充分的理由不让容器适配器提供它吗?

Does anyone know why std::queue, std::stack, and std::priority_queue don't provide a clear() member function? I have to fake one like this:

std::queue<int> q;
// time passes...
q = std::queue<int>();  // equivalent to clear()

IIRC, clear() is provided by everything that could serve as the underlying container. Is there a good reason to not have the container adaptors provide it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

陌上青苔 2024-07-20 02:55:12

好吧,我认为这是因为 clear 不被认为是对队列、priority_queue 或堆栈的有效操作(顺便说一句,deque 不是适配器,而是容器)。

使用容器的唯一原因
适配器队列而不是容器
deque 是为了明确你是
仅执行队列操作,以及
没有其他操作。 (来自队列上的 sgi 页面)

因此,在使用队列时,您可以do 是推送/弹出元素; 清除队列可以被视为违反了 FIFO 概念。 因此,如果您需要清除队列,也许它不是真正的队列,您最好使用双端队列。

不过,这种想法有点狭隘,我认为像你这样清理队列是很公平的。

Well, I think this is because clear was not considered a valid operation on a queue, a priority_queue or a stack (by the way, deque is not and adaptor but a container).

The only reason to use the container
adaptor queue instead of the container
deque is to make it clear that you are
performing only queue operations, and
no other operations. (from the sgi page on queue)

So when using a queue, all you can do is push/pop elements; clearing the queue can be seen as a violation of the FIFO concept. Consequently, if you need to clear your queue, maybe it's not really a queue and you should better use a deque.

However, this conception of things is a little narrow-minded, and I think clearing the queue as you do is fair enough.

独自←快乐 2024-07-20 02:55:12

双端队列有clear()。 参见,例如,http://www.cplusplus.com/reference/stl/双端队列/clear.html

然而,队列却没有。 但为什么你会选择队列而不是双端队列呢?

使用容器的唯一原因
适配器队列而不是容器
deque 是为了明确你是
仅执行队列操作,以及
没有其他操作。

(http://www.sgi.com/tech/stl/queue.html)

所以我猜clear() 不是一个队列操作。

Deque has clear(). See, e.g., http://www.cplusplus.com/reference/stl/deque/clear.html.

However, queue does not. But why would you choose queue over deque, anyway?

The only reason to use the container
adaptor queue instead of the container
deque is to make it clear that you are
performing only queue operations, and
no other operations.

(http://www.sgi.com/tech/stl/queue.html)

So I guess clear() is not a queue operation, then.

放肆 2024-07-20 02:55:12

我想说这是因为容器适配器不是容器。

I'd say it's because container adaptors are not containers.

小鸟爱天空丶 2024-07-20 02:55:12

只要您继承自它,您就可以清除队列(以及 std::stack 和priority_queue)。 故意对容器进行保护以允许这样做。

#include <queue>

using namespace std;

class clearable_queue : public queue<int>
{
public:
  void clear()
    {
      // the container 'c' in queues is intentionally left protected
      c.clear();
    }
};   

int main(int argc, char** argv)
{
  clearable_queue a;
  a.clear();
}

You CAN clear queues (and std::stack and priority_queue), as long as you inherit from it. The container is intentionally left protected to allow this.

#include <queue>

using namespace std;

class clearable_queue : public queue<int>
{
public:
  void clear()
    {
      // the container 'c' in queues is intentionally left protected
      c.clear();
    }
};   

int main(int argc, char** argv)
{
  clearable_queue a;
  a.clear();
}
沉睡月亮 2024-07-20 02:55:12

我认为这取决于实现 - 直到最近 Microsoft STL 还没有明确几个容器。 (现在确实如此,例如这个快速谷歌结果

但是,clear()通常只是一个调用擦除(begin(), end()),因此实现您自己的等效项并使用它。

我认为该标准将清除指的是在迭代器范围内进行擦除,因此以上是大多数实现将提供的内容。 (例如 Dinkumware 的

I think it depends on the implementation - until recently Microsoft STL didn't have clear on several containers. (it does now, eg this quick google result)

However, clear() is often simply a call to erase(begin(), end()), so implement your own equivalent and use that instead.

I think the standard refers to clear as erasing over an iterator range, so the above is what most implementations will provide. (eg Dinkumware's)

梦里兽 2024-07-20 02:55:12

std::queue、std::deque 和 std::priority_queue 是容器适配器,仅提供少量方法来访问底层容器。

您可以清除底层容器,只要您可以访问它即可。 为此,请创建底层容器以传递给 apadptor 构造函数。 例如:

std::deque< int > d;
std::queue< int > q( d );

... time passes ...

d.clear();

编辑:附加信息

我还应该警告您在这里小心行事,因为调用底层容器上的方法可能会破坏适配器所做的假设。 在这方面,您目前清理队列的方式似乎更可取。

std::queue, std::deque, and std::priority_queue are container adaptors and only provide a small number of methods to access the underlying container.

You can clear the underlying container, so long as you can access it. To do this, create the underlying container to pass in to the apadptor constructor. For example:

std::deque< int > d;
std::queue< int > q( d );

... time passes ...

d.clear();

Edit: additional info

I should also have warned you to tread carefully here as calling methods on the underlying container may break assumptions made by the adaptor. In that respect, the way you are currently clearng the queue seems preferable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文