使用具有两个字段和一对字段的结构有什么区别?

2021-12-23 00:00:00 struct c++ std-pair std

使用具有两个字段和一对字段的结构在内存分配和效率方面有什么区别?

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.

相关文章