why std::stack has separate top() and pop()
SGI explanation: http://www.sgi.com/tech/stl/stack.html One might wonder why pop() returns void, instead of value_type. That is, why must one use top() and pop() to examine and remove the top element, instead of combining the two in a single member function? In fact, there is a good reason for this design. If pop() returned the top element, it would have to return by value rather than by reference: return by reference would create a dangling pointer. Return by value, however, is inefficient: it involves at least one redundant copy constructor call. Since it is impossible for pop() to return a value in such a way as to be both efficient and correct, it is more sensible for it to return no value at all and to require clients to use top() to inspect the value at the top of the stack.
std::stack < T > is a template. If pop() returned the top element, it would have to return by value rather than by reference as per the of above explanation. That means, at the caller side it must be copied in an another T type of object. That involves a copy constructor or copy assignment operator call. What if this type T is sophisticated enough and it throws an exception during copy construction or copy assignment? In that case, the rvalue, i.e. the stack top (returned by value) is simply lost and there is no other way to retrieve it from the stack as the stack's pop operation is successfully completed!
the 2nd point might not be the reason of the initial design as STL is introduced into C++ before exceptions; it's a good point to make though.
why std::stack has separate top() and pop()的更多相关文章
- C++ std::stack
std::stack template <class T, class Container = deque<T> > class stack; LIFO stack Stack ...
- C++ std::stack 基本用法
#include <iostream> #include <string> #include <stack> // https://zh.cppreference. ...
- 从deque到std::stack,std::queue,再到iOS 中NSArray(CFArray)
从deque到std::stack,std::queue,再到iOS 中NSArray(CFArray) deque deque双端队列,分段连续空间数据结构,由中控的map(与其说map,不如说是数 ...
- 设计一个Stack,要求Push、Pop、获取最大最小值时间复杂度都为O(1)
面试的时候,面试官让设计一个栈,要求有Push.Pop和获取最大最小值的操作,并且所有的操作都能够在O(1)的时间复杂度完成. 当时真没啥思路,后来在网上查了一下,恍然大悟,只能恨自己见识短浅.思路不 ...
- 华为笔试题--LISP括号匹配 解析及源码实现
在17年校招中3道题目AC却无缘华为面试,大概是华为和东华互不待见吧!分享一道华为笔试原题,共同进步! ************************************************ ...
- C++并发编程学习笔记
// // main.cpp // test1 // // Created by sofard on 2018/12/27. // Copyright © 2018年 dapshen. All ...
- C++并发编程 02 数据共享
在<C++并发编程实战>这本书中第3章主要将的是多线程之间的数据共享同步问题.在多线程之间需要进行数据同步的主要是条件竞争. 1 std::lock_guard<std::mute ...
- [POJ2337]Catenyms
题目大意: 定义一个catenym是一对单词,满足第一个单词的末尾字符与第二个单词的开头字符相等. 定义复合catenym是一些单词,满足第i个单词的末尾字符与第i+1个单词的开头字符相等. 给你n个 ...
- 北京清北 综合强化班 Day3
括号序列(bracket) Time Limit:1000ms Memory Limit:128MB 题目描述 LYK有一个括号序列,但这个序列不一定合法. 一个合法的括号序列如下: ()是合法的 ...
随机推荐
- iOS-代理托付的使用
#import "Rigester_ViewController.h" #import "Rigester_ViewController.h" @interfa ...
- 新西兰天维网登录发送明文password
新西兰比較有人气的华人社区站点是天维网(新西兰天维网),是这边华人用中文吐槽常常上的论坛,也是华人之间各种交易(比方买卖二手车)的集散地.上次非诚勿扰新西兰专场就是天维网承办的宣传和报名.来新西兰定居 ...
- c# combobox 绑定枚举方式
建立一个类 : using System; using System.Collections.Generic; using System.Linq; using System.Text; using ...
- Fragment 生命周期:
Fragment每个生命周期方法的意义.作用(注意红色的不是生命周期方法):setUserVisibleHint():设置Fragment可见或者不可见时会调用此方法.在该方法里面可以通过调用getU ...
- Open-sourcing LogDevice, a distributed data store for sequential data
https://logdevice.io/blog/2018/09/12/open-sourcing-announcement.html September 12, 2018 We are exc ...
- 一阶 斜率 二阶 原函数的粗糙度 roughness
1 2 损失函数+惩罚函数 2阶导数
- 2018年东北农业大学春季校赛 E wyh的集合 【数学】
题目链接 https://www.nowcoder.com/acm/contest/93/F 思路 其实容易知道在两个不同集合里 假设元素个数 分别为 a b 然后对于第一个集合里的每一个元素 都可以 ...
- Django+ajax+jsonp实现借口调用文本处理
首页 提交 <script src="/static/jquery-2.1.4.min.js"></script> <script type=&quo ...
- HDU2296 Ring —— AC自动机 + DP
题目链接:https://vjudge.net/problem/HDU-2296 Ring Time Limit: 2000/1000 MS (Java/Others) Memory Limit ...
- HDU2825 Wireless Password —— AC自动机 + 状压DP
题目链接:https://vjudge.net/problem/HDU-2825 Wireless Password Time Limit: 2000/1000 MS (Java/Others) ...