What is the difference between using a struct with two fields and a pair?(使用具有两个字段和一对字段的结构有什么区别?)
问题描述
使用具有两个字段和一对字段的结构在内存分配和效率方面有什么区别?
What is the difference regarding memory allocation and efficiency between using a struct with two fields and a pair?
推荐答案
std::pair
提供预先编写的构造函数和比较运算符.这也允许它们存储在 std::map 之类的容器中,而无需编写,例如,复制构造函数或通过 operator <
的严格弱排序(例如 std 所要求的::地图
).如果你不写它们,你就不会犯错(还记得严格弱排序是如何工作的吗?)所以使用 std::pair
更可靠.
std::pair
provides pre-written constructors and comparison operators. This also allows them to be stored in containers like std::map without you needing to write, for example, the copy constructor or strict weak ordering via operator <
(such as required by std::map
). If you don't write them you can't make a mistake (remember how strict weak ordering works?) so it's more reliable just to use std::pair
.
这篇关于使用具有两个字段和一对字段的结构有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用具有两个字段和一对字段的结构有什么区别?


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