C ++ stl pop不返回 [英] C++ stl pop doesn't return

查看:192
本文介绍了C ++ stl pop不返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经历过,stl pop函数不返回任何东西。



如何返回栈顶的元素并删除它? >

top函数不是一个好的解决方案,因为它只返回顶级元素的引用...

解决方案

如果在弹出后不再需要该元素,

  with 
container.top();
// remove it
container.pop();

否则复制对象:

  Element e = container.top(); 
container.pop(); // remove it
//随意使用e,你复制了它

如果你需要真正删除它手动,那么你没有使用任何类型的智能指针,这将问题移到另一个话题。


I have experienced, that the stl pop function doesn't return anything.

How can I return the top element of my stack and delete it?

The top function is not a good solution, because it returns only a reference of the top element...

解决方案

If you don't need the element anymore after popping it just

// use element with
container.top();
// remove it
container.pop();

Otherwise make a copy of the object:

Element e = container.top();
container.pop(); // remove it
// feel free to use e, you made a copy of it

If you need to really delete it manually then you are not using any kind of smart pointer, which moves the problem to another topic.

这篇关于C ++ stl pop不返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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