What is the difference between using a struct with two fields and a pair?

后端 未结 6 603
刺人心
刺人心 2020-12-03 06:42

What is the difference regarding memory allocation and efficiency between using a struct with two fields and a pair?

6条回答
  •  没有蜡笔的小新
    2020-12-03 07:29

    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.

提交回复
热议问题