为什么pop()需要一个参数? [英] Why should pop() take an argument?

查看:200
本文介绍了为什么pop()需要一个参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

快速背景

我是一名Java开发人员,在我的免费/无聊时间里一直在使用C ++。

Quick background
I'm a Java developer who's been playing around with C++ in my free/bored time.

前言

在C ++中,您通常会看到引用参数的参数:

Preface
In C++, you often see pop taking an argument by reference:

void pop(Item& removed);

我理解,用你删除的参数填充是很好的。这对我来说是完全合理的。这样,请求删除顶部项的人可以查看删除的内容。

I understand that it is nice to "fill in" the parameter with what you removed. That totally makes sense to me. This way, the person who asked to remove the top item can have a look at what was removed.

但是,如果我在Java中执行此操作,做这样的事情:

However, if I were to do this in Java, I'd do something like this:

Item pop() throws StackException;

这样,在弹出后,我们返回:NULL作为结果,项目或异常

This way, after the pop we return either: NULL as a result, an Item, or an exception would be thrown.

我的C ++课本给我看了上面的例子,但我看到很多堆栈实现没有参数( stl stack )。

My C++ text book shows me the example above, but I see plenty of stack implementations taking no arguments (stl stack for example).

问题 br>
如何在C ++中实现pop函数?

The Question
How should one implement the pop function in C++?

奖金

为什么? / p>

The Bonus
Why?

推荐答案

要回答这个问题:你不应该在C ++中实现pop函数,因为它已经被STL实现了。 std :: stack 容器适配器提供方法 top 来获取对堆栈顶部元素的引用,方法 pop 删除顶部元素。注意,单独的 pop 方法不能用于执行这两个操作。

To answer the question: you should not implement the pop function in C++, since it is already implemented by the STL. The std::stack container adapter provides the method top to get a reference to the top element on the stack, and the method pop to remove the top element. Note that the pop method alone cannot be used to perform both actions, as you asked about.

为什么


  1. 异常安全:Herb Sutter对 GotW#82

  2. 单一责任原则:也在GotW#82中提到。 顶部负责一个责任, pop 负责其他。

  3. 不要为不需要的东西付费:对于某些代码,可能只需检查顶部元素,然后将其弹出,而不必创建元素的(可能昂贵的)副本。 (这在 SGI STL 文档中有提及。)

  1. Exception safety: Herb Sutter gives a good explanation of the issue in GotW #82.
  2. Single-responsibility principle: also mentioned in GotW #82. top takes care of one responsibility and pop takes care of the other.
  3. Don't pay for what you don't need: For some code, it may suffice to examine the top element and then pop it, without ever making a (potentially expensive) copy of the element. (This is mentioned in the SGI STL documentation.)

任何希望获取元素副本的代码都可以免费支付:

Any code that wishes to obtain a copy of the element can do this at no additional expense:

Foo f(s.top());
s.pop();

此外,这个讨论可能很有趣。

如果你要实现pop返回该值,无论是通过值返回还是将其写入out参数都无关紧要。大多数编译器都会实施 RVO ,这将优化按值返回方法,使其效率与复制到输出参数方法。只要记住,任何一个可能会比使用top()或front()检查对象的效率更低,因为在这种情况下绝对没有复制完成。

If you were going to implement pop to return the value, it doesn't matter much whether you return by value or write it into an out parameter. Most compilers implement RVO, which will optimize the return-by-value method to be just as efficient as the copy-into-out-parameter method. Just keep in mind that either of these will likely be less efficient than examining the object using top() or front(), since in that case there is absolutely no copying done.

这篇关于为什么pop()需要一个参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆