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
.
这篇关于使用具有两个字段和一对字段的结构有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用具有两个字段和一对字段的结构有什么区别?


基础教程推荐
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 我有静态或动态 boost 库吗? 2021-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 常量变量在标题中不起作用 2021-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 这个宏可以转换成函数吗? 2022-01-01