C++ 将 std::stack .pop() 方法的结果存储到变量中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12206242/
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-27 16:01:46  来源:igfitidea点击:

Store results of std::stack .pop() method into a variable

c++stack

提问by PinkElephantsOnParade

I'd like to do the following:

我想做以下事情:

std::stack <int> s;
int h = 0;
s.push(2);
h = s.pop();

Such as to have hhold the value 2. When I try my method, I get “void value not ignored as it ought to be”.

例如h持有值 2。当我尝试我的方法时,我得到“没有被忽略的无效值,因为它应该是”。

Is this not the intention of the .pop()method? What is the preferred way to do this?

这不是.pop()方法的意图吗?这样做的首选方法是什么?

回答by Kerrek SB

The standard library containers separate top()and pop(): top()returns a reference to the top element, and pop()removes the top element. (And similarly for back()/pop_back()etc.).

标准库容器分离top()pop():top()返回对顶部元素的引用,并pop()删除顶部元素。(对于back()/pop_back()等也类似)。

There's a good reason for this separation, and not have popremove the top element andreturn it: One guiding principle of C++ is that you don't pay for what you don't need. A single function would have no choice but to return the element by value, which may be undesired. Separating concerns gives the user the most flexibility in how to use the data structure. (See note #3 in the original STL documentation.)

这种分离有一个很好的理由,而不是pop删除顶部元素返回它:C++ 的一个指导原则是你不需要为不需要的东西付费。单个函数别无选择,只能按值返回元素,这可能是不希望的。分离关注点为用户提供了如何使用数据结构的最大灵活性。(请参阅原始 STL 文档注释 #3 。)

(As a curiousum, you may notice that for a concurrentcontainer, a pop-like function is actually forcedto remove and return the top value atomically, since in a concurrent context, there is no such notion as "being on top" (or "being empty" for that matter). This is one of the obvious examples of how concurrent data structures take a significant performance hit in order to provide their guarantees.)

(奇怪的是,您可能会注意到,对于并发容器,-likepop函数实际上被迫以原子方式删除并返回顶部值,因为在并发上下文中,没有“位于顶部”(或“为空”)。这是并发数据结构如何对性能造成重大影响以提供保证的明显例子之一。)

回答by Marco167

You can use:

您可以使用:

h = s.top();

then after that use(if you want to remove the most recent value otherwise do nothing)

然后在使用之后(如果你想删除最新的值,否则什么都不做)

 s.pop();

It works the same way!!

它的工作方式相同!!

回答by user10059288

You can actually use s.top()to store the element and then pop it using

您实际上可以使用s.top()来存储元素,然后使用

s.pop().

use

int h=s.top();
s.pop()

instead of

代替

int h=s.pop()

You cannot directly assign s.pop()to some data type, as s.pop()removes element from stack and returns nothing.

您不能直接分配s.pop()给某些数据类型,因为s.pop()从堆栈中删除元素并且不返回任何内容。

回答by rafiqul islam

S.pop()does not return any value. Because pop()is void function. If you want to see the top of the stack then it will be S.top(). If you store this value then write value = S.top().

S.pop()不返回任何值。因为pop()是空函数。如果您想查看堆栈的顶部,那么它将是S.top(). 如果您存储此值,则写入value = S.top().