pop_back() return value?(pop_back() 返回值?)
问题描述
为什么 pop_back()
没有返回值?我在谷歌上搜索过这个,发现它更有效.这是在标准中这样做的唯一原因吗?
Why doesn't pop_back()
have a return value? I have Googled regarding this and found out that it makes it more efficient. Is this the only reason for making it so in the standard?
推荐答案
我认为复制最后一个对象的实例可能会引发异常这一事实与此有关.这样做时,您将丢失对象,因为 pop_back() 确实将其从容器中删除.用几行代码更好:
I think there is something related to the fact that copying an instance of the last object could throw an exception. When doing so, you're losing your object, since pop_back() did remove it from your container. Better with a few lines of code:
std::vector<AnyClass> holds = {...} ;
try {
const AnyClass result = holds.pop_back(); // The copy Ctor throw here!
} catch (...)
{
// Last value lost here.
}
这篇关于pop_back() 返回值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:pop_back() 返回值?


- STL 中有 dereference_iterator 吗? 2022-01-01
- 如何对自定义类的向量使用std::find()? 2022-11-07
- 一起使用 MPI 和 OpenCV 时出现分段错误 2022-01-01
- 使用/clr 时出现 LNK2022 错误 2022-01-01
- 从python回调到c++的选项 2022-11-16
- 近似搜索的工作原理 2021-01-01
- Stroustrup 的 Simple_window.h 2022-01-01
- 与 int by int 相比,为什么执行 float by float 矩阵乘法更快? 2021-01-01
- C++ 协变模板 2021-01-01
- 静态初始化顺序失败 2022-01-01