什么是 std::pair?

2021-12-24 00:00:00 c++ stl std-pair boost

std::pair 有什么用,我为什么要使用它,boost::compressed_pa??ir 有什么好处?

解决方案

std::pair 是一种将两个值组合为一个对象的数据类型.std::map 将其用作密钥,值对.

在您学习时pair,您可以查看 tuple.它就像 pair ,但用于对任意数量的值进行分组.tuple 是 TR1 的一部分,许多编译器已经将它包含在他们的标准库实现中.

此外,请参阅 Pete Becker 所著《C++ 标准库扩展:教程和参考》一书的第 1 章元组",ISBN-13:9780321412997,以获得详尽的解释.>

What is std::pair for, why would I use it, and what benefits does boost::compressed_pair bring?

解决方案

std::pair is a data type for grouping two values together as a single object. std::map uses it for key, value pairs.

While you're learning pair, you might check out tuple. It's like pair but for grouping an arbitrary number of values. tuple is part of TR1 and many compilers already include it with their Standard Library implementations.

Also, checkout Chapter 1, "Tuples," of the book The C++ Standard Library Extensions: A Tutorial and Reference by Pete Becker, ISBN-13: 9780321412997, for a thorough explanation.

相关文章