具有 2 个单元的结构与 std::pair?

发布于 2024-09-16 19:41:43 字数 628 浏览 9 评论 0原文

可能的重复:
有什么区别使用具有两个字段和一对字段的结构之间?

亲爱的大家,

我有一个关于对和结构的小问题。使用 std::pair 而不是具有两个单元的结构有什么优势吗? 我已经使用过一段时间了,但主要问题是可读性: 例如,如果您想表示一个双元组(int“label”,double“value”),您可以使用 a :

typedef std::pair<int,double> myElem;

或 a

typedef struct {
    int label;
    double value;
} myElem;

如果您的语句具有“语义”意义(您将始终知道 x. x.first 的情况并非如此)。

但是,我认为使用pair有一个优点。是性能更好还是其他什么?

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

Dear all,

I have a little question about pairs and struct. Is there any advantage to use a std::pair instead of a struct with two cells ?
I have used pairs for a while but the main problem is readability :
If you want to represent for example a duple (int "label", double "value") you can use either a :

typedef std::pair<int,double> myElem;

or a

typedef struct {
    int label;
    double value;
} myElem;

The code becomes more readable if your statements have a "semantic" sense (you will always know what x.label is. that's not the case with x.first).

However, I guess there is an advantage using pair. Is it more performant or something else?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

相权↑美人 2024-09-23 19:41:43

就性能而言:它不太可能改变任何东西,你只是在糖衣而已。

就可用性而言,我宁愿使用自定义结构,可以通过这种方式声明(顺便说一句):

struct MyElement
{
  int label;
  double value;
};

我是强类型的坚定支持者,并且我更喜欢“真正的”结构(更好的是类)每当它不仅仅是一个转瞬即逝的事物时,它就比一个临时元组更重要。

主要是因为:

  • 正如您所指出的 firstsecond 没有太多意义
  • 您无法将方法/其他字段添加到 std::pair
  • 您无法将类不变量添加到 std::pair

总而言之,我确实认为使用一种通用的元组的自定义专用结构可以使维护受益。

In terms of performance: it's unlikely to change anything, you're just sugar coating it.

In terms of usability, I would rather use a custom struct, which can be declared this way (by the way):

struct MyElement
{
  int label;
  double value;
};

I am a strong proponent of strong typing, and I much prefer a "real" structure (and better yet, class) than an ad-hoc tuple whenever it's more than a fleeting thing.

Mainly because:

  • As you noted first and second don't carry much meaning
  • You cannot add methods / other fields to a std::pair
  • You cannot add class invariants to a std::pair

All in all, I really think that maintenance benefits from using a custom dedicated structure that a one-size-fits-them-all tuple.

冷夜 2024-09-23 19:41:43

pair 被实现为模板化的struct。它为您提供了创建(通常是异构的)对的简写。此外,可以与 pair 一起使用的类型也有一些限制:

类型要求

T1 和 T2 必须同时
是可分配的模型。额外的
操作有额外的
要求。配对的默认值
仅当两者都有效时才可以使用构造函数
T1 和 T2 是默认可构造的,
仅当 T1 都满足时才可使用运算符 ==
和 T2 是相等可比较的,并且
运算符<仅当两个 T1
和 T2 是 LessThanComparable。

(来自 SGI STL std::pair 文档)

如果类型不遵循任何这些约束或者您不关心它们,那么定义您自己的 POD 可能是有意义的。

最后,我想这是个人选择/编码风格的问题。

A pair is implemented as a templated struct. It provides you with a shorthand for creating a (typically heterogenous) pair. Also, there are some constraints on the types that you can use with a pair:

Type requirements

T1 and T2 must both
be models of Assignable. Additional
operations have additional
requirements. Pair's default
constructor may only be used if both
T1 and T2 are DefaultConstructible,
operator== may only be used if both T1
and T2 are EqualityComparable, and
operator< may only be used if both T1
and T2 are LessThanComparable.

(from SGI STL std::pair documentation)

Defining your own POD may make sense if the types do not follow any of these constraints or if you do not care about them.

Finally, I guess it is a matter of personal choice/coding style.

嘿咻 2024-09-23 19:41:43

它的主要优点是它是通用的。例如,当您从 std::map 检索某些内容时,您将获得键和关联值作为 std::pair 中的第一项和第二项。

同样,当您使用 std::equal_range 在集合中查找一组相等的值时,您将获得范围开头和结尾的迭代器,作为 std::equal_range 中的第一项和第二项::配对。

很难想象有意义的标签可以同时适用于这两者,因此他们选择了一些意义不大,但至少不会误导的标签。使用“key”和“data”适用于 std::map,但会误导 std::equal_range(如果您切换到 lower_bound 之类的内容)upper_bound 以使它们对 std::equal_range 更有意义,对于 std::map 中的项目同样是错误的>)。

Its primary advantage is that it's generic. For example, when you retrieve something from an std::map, you get the key and the associated value as the first and second items in an std::pair.

Likewise, when you use std::equal_range to find a set of equal values in a collection, you get iterators to the beginning and end of the range as the first and second items in an std::pair.

It's hard to imagine meaningful labels that would apply to both of these, so they settled for a couple that don't mean very much, but at least aren't misleading. Using 'key' and 'data' would work for std::map, but be misleading for std::equal_range (and if you switched to something like lower_bound and upper_bound to make them more meaningful for std::equal_range, it'd be equally wrong for items in std::map).

雨后咖啡店 2024-09-23 19:41:43

但是,我认为使用pair有一个优势。是性能更好还是其他什么?

我对此表示怀疑,毕竟实例化的 std::pair 只是一个 struct 。不过,std::pair 附带了 operator< 定义。但对于只有两个成员的 struct 来说,这对于您自己来说应该不会太难。

因此,我通常按照您的推理进行操作:具有专用成员名称的 structfirstsecond 更容易阅读。

However, I guess there is an advantage using pair. Is it more performant or something else?

I doubt it, an instantiated std::pair is just a struct, after all. std::pair comes with operator< defined, though. But that shouldn't be too hard to do yourself for a struct with only two members.

So I usually do as you reasoned: a struct with dedicated member names is easier to read than first and second.

溺渁∝ 2024-09-23 19:41:43

通过 std::pair,您可以自由使用 STL 中的函子,例如 select1st 或 select2nd。同样,它允许您在配对中使用其他通用函数,例如运算符<和其他人。不可否认,随着 boost/tr1 的出现,您可以通过使用 bind 实现大致相同的效果。

不过,您关于可读性的观点是非常正确的。

With a std::pair, you are free to use functors from the STL such as select1st or select2nd. Likewise it allows you to use other generic functions with your pair, such as operator< and others. Admittedly, with the advent of boost/tr1, you could achieve much the same effect by using bind.

Your point about readability is very true though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文