具有 2 个单元的结构与 std::pair?
可能的重复:
有什么区别使用具有两个字段和一对字段的结构之间?
亲爱的大家,
我有一个关于对和结构的小问题。使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
就性能而言:它不太可能改变任何东西,你只是在糖衣而已。
就可用性而言,我宁愿使用自定义结构,可以通过这种方式声明(顺便说一句):
我是强类型的坚定支持者,并且我更喜欢“真正的”结构(更好的是类)每当它不仅仅是一个转瞬即逝的事物时,它就比一个临时元组更重要。
主要是因为:
first
和second
没有太多意义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):
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:
first
andsecond
don't carry much meaningstd::pair
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.
pair
被实现为模板化的struct
。它为您提供了创建(通常是异构的)对的简写。此外,可以与pair
一起使用的类型也有一些限制:(来自 SGI STL
std::pair
文档)如果类型不遵循任何这些约束或者您不关心它们,那么定义您自己的 POD 可能是有意义的。
最后,我想这是个人选择/编码风格的问题。
A
pair
is implemented as a templatedstruct
. 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 apair
:(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.
它的主要优点是它是通用的。例如,当您从
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 anstd::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 anstd::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 forstd::equal_range
(and if you switched to something likelower_bound
andupper_bound
to make them more meaningful forstd::equal_range
, it'd be equally wrong for items instd::map
).我对此表示怀疑,毕竟实例化的 std::pair 只是一个 struct 。不过,
std::pair
附带了operator<
定义。但对于只有两个成员的struct
来说,这对于您自己来说应该不会太难。因此,我通常按照您的推理进行操作:具有专用成员名称的
struct
比first
和second
更容易阅读。I doubt it, an instantiated
std::pair
is just astruct
, after all.std::pair
comes withoperator<
defined, though. But that shouldn't be too hard to do yourself for astruct
with only two members.So I usually do as you reasoned: a
struct
with dedicated member names is easier to read thanfirst
andsecond
.通过 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.