使用具有两个字段和一对字段的结构在内存分配和效率方面有什么区别?
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
.
这篇关于使用具有两个字段和一对字段的结构有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!