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有一个括号序列,但这个序列不一定合法. 一个合法的括号序列如下: ()是合法的 ...
随机推荐
- ASP.NET动态网站制作(6)-- JS(1)
前言:JS的第一节课,在Visual Studio 2013中编写及运行.新建项目->Web->ASP.NET Web应用程序->Empty,打开后在项目下添加新建css文件夹和js ...
- zookeeper启动流程简单梳理
等着測试童鞋完工,顺便里了下zookeeper的启动流程 zk3.4.6 启动脚本里面 nohup "$JAVA" "-Dzookeeper.log.dir=${ZOO_ ...
- EasyNVR摄像机H5流媒体服务器在windows上批处理脚本自动以管理员权限运行
很多时候, 我们需要以管理员权限来运行批处理脚本, 比如操作 windows 服务. EasyNVR 中提供安装服务的批处理脚本, 运行这个bat文件, 自动将 EasyNVR 以 windows 服 ...
- 九度OJ 1057:众数 (基础题)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:8431 解决:2819 题目描述: 输入20个数,每个数都在1-10之间,求1-10中的众数(众数就是出现次数最多的数,如果存在一样多次数的 ...
- 【题解】CF45G Prime Problem
[题解]CF45G Prime Problem 哥德巴赫板子题? \(\frac{n(n+1)}{2}\)若是质数,则不需要分了. 上式 若是奇数,那么拆成2和另一个数. 上式 若是偶数吗,直接\(O ...
- x86 寻址学习
x86 寻址方式众多,什么直接寻址.间接寻址.基址寻址.基址变址寻址等等让人眼花缭乱,而 AT&T 语法对内存寻址方式做了一个很好的统一,其格式为 section:displacement(b ...
- pyinstaller使用
python pyinstaller.py [-Fw] ???.py -F 将相关配件(dll.oxc)合成到单个exe文件 -w exe启动时不打开console窗口
- Maven简介(六)——Dependency
7 Dependency介绍 http://elim.iteye.com/category/269897 7.1 依赖的传递性 当项目A依赖于B,而B又依赖于C的时候,自然的A会依赖 ...
- C++配置Opencv
https://blog.csdn.net/qq_17550379/article/details/78201442
- 在js实现矩阵转置
var arr=[[2,4,6,8],[8,9,0,-1],[9,6,2,1]]; //定义一个新的数组 var arr2=[]; for(var i=0;i<arr[0].length;i++ ...