1. 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.

  2. 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()的更多相关文章

  1. C++ std::stack

    std::stack template <class T, class Container = deque<T> > class stack; LIFO stack Stack ...

  2. C++ std::stack 基本用法

    #include <iostream> #include <string> #include <stack> // https://zh.cppreference. ...

  3. 从deque到std::stack,std::queue,再到iOS 中NSArray(CFArray)

    从deque到std::stack,std::queue,再到iOS 中NSArray(CFArray) deque deque双端队列,分段连续空间数据结构,由中控的map(与其说map,不如说是数 ...

  4. 设计一个Stack,要求Push、Pop、获取最大最小值时间复杂度都为O(1)

    面试的时候,面试官让设计一个栈,要求有Push.Pop和获取最大最小值的操作,并且所有的操作都能够在O(1)的时间复杂度完成. 当时真没啥思路,后来在网上查了一下,恍然大悟,只能恨自己见识短浅.思路不 ...

  5. 华为笔试题--LISP括号匹配 解析及源码实现

    在17年校招中3道题目AC却无缘华为面试,大概是华为和东华互不待见吧!分享一道华为笔试原题,共同进步! ************************************************ ...

  6. C++并发编程学习笔记

    // //  main.cpp //  test1 // //  Created by sofard on 2018/12/27. //  Copyright © 2018年 dapshen. All ...

  7. C++并发编程 02 数据共享

    在<C++并发编程实战>这本书中第3章主要将的是多线程之间的数据共享同步问题.在多线程之间需要进行数据同步的主要是条件竞争. 1  std::lock_guard<std::mute ...

  8. [POJ2337]Catenyms

    题目大意: 定义一个catenym是一对单词,满足第一个单词的末尾字符与第二个单词的开头字符相等. 定义复合catenym是一些单词,满足第i个单词的末尾字符与第i+1个单词的开头字符相等. 给你n个 ...

  9. 北京清北 综合强化班 Day3

    括号序列(bracket) Time Limit:1000ms   Memory Limit:128MB 题目描述 LYK有一个括号序列,但这个序列不一定合法. 一个合法的括号序列如下: ()是合法的 ...

随机推荐

  1. iOS开发 僵尸调试

    本文转载至 http://blog.sina.com.cn/s/blog_a843a8850101dxin.html   引自:http://blog.csdn.net/likendsl/articl ...

  2. Objective-C中NSString转NSNumber的方法

    本文转载至 http://www.linuxidc.com/Linux/2013-02/78866.htm 在Objective-C中,以数字格式组成的字符串经常需要转换为NSNumber对象后再使用 ...

  3. cf-341C Iahub and Permutations

    C. Iahub and Permutations time limit per test 1 second memory limit per test 256 megabytes input sta ...

  4. idea创建普通java项目以及maven创建项目过程(转)

    1. idea创建一个普通项目流程 http://blog.csdn.net/testcs_dn/article/details/52303941 2. idea创建maven项目流程 http:// ...

  5. 九度OJ 1148:Financial Management(财务管理) (平均数)

    与1141题相同. 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:843 解决:502 题目描述: Larry graduated this year and finally has a ...

  6. Asynchronous_method_invocation 异步方法调用 让步 yielding

    zh.wikipedia.org/wiki/同步 [同步不同事件发生 时间一致] 同步(英语:Synchronization),指在一个系统中所发生的事件(event),之间进行协调,在时间上出现一致 ...

  7. cocos2d-js v3.1的坑

    前几天因为要用到cc.pool,所以就换了v3.1版本,结果连生成apk的出错(cocos code ide), log显示为:error: relocation overflow in R_ARM_ ...

  8. Django框架ORM常用字段汇总_模型层

    与数据类型相关的字段 CharField 作用:字符串字段, 用于较短的字符串. 参数:CharField 要求必须有一个参数 maxlength, 用于从数据库层和Django校验层限制该字段所允许 ...

  9. 【JAVA学习】struts2的action中使用session的方法

    尊重版权:http://hi.baidu.com/dillisbest/item/0bdc35c0b477b853ad00efac 在Struts2里,假设须要在Action中使用session.能够 ...

  10. rbx1包里机器人仿真程序的实践

     git clone https://github.com/pirobot/rbx1.git 1.打开一个终端 cd ~/catkin_ws/ catkin_make source ./devel/s ...