为什么在C ++中使用元组不是更常见? [英] Why is the use of tuples in C++ not more common?

查看:104
本文介绍了为什么在C ++中使用元组不是更常见?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么没有人似乎在C ++中使用元组, Boost Tuple Library 或TR1的标准库?我读了很多C ++代码,很少看到元组的使用,但我经常看到很多地方,元组会解决很多问题(通常从函数返回多个值)。

Why does nobody seem to use tuples in C++, either the Boost Tuple Library or the standard library for TR1? I have read a lot of C++ code, and very rarely do I see the use of tuples, but I often see lots of places where tuples would solve many problems (usually returning multiple values from functions).

Tuples允许你这样做各种各样的酷事:

Tuples allow you to do all kinds of cool things like this:

tie(a,b) = make_tuple(b,a); //swap a and b

这肯定比这更好:

temp=a;
a=b;
b=temp;

当然,你总是可以这样做:

Of course you could always do this:

swap(a,b);

但是如果你想旋转三个值呢?您可以使用元组:

But what if you want to rotate three values? You can do this with tuples:

tie(a,b,c) = make_tuple(b,c,a);

元组也使得从函数返回多个变量更容易,这可能是更常见的情况比交换值。使用对返回值的引用当然不是很优雅。

Tuples also make it much easier to return multiple variable from a function, which is probably a much more common case than swapping values. Using references to return values is certainly not very elegant.

有没有我不想的元组的大缺点?如果不是,为什么他们很少使用?它们更慢吗?还是只是人们不习惯他们?使用元组是个好主意吗?

Are there any big drawbacks to tuples that I'm not thinking of? If not, why are they rarely used? Are they slower? Or is it just that people are not used to them? Is it a good idea to use tuples?

推荐答案

因为它还不标准。任何非标准的都有更高的障碍。 Boost的片段已经变得流行,因为程序员正在为他们咆哮。 (哈希映射跳跃)。但是元组是方便的,这不是一个压倒性的明确的胜利,人们对此感到烦恼。

Because it's not yet standard. Anything non-standard has a much higher hurdle. Pieces of Boost have become popular because programmers were clamoring for them. (hash_map leaps to mind). But while tuple is handy, it's not such an overwhelming and clear win that people bother with it.

这篇关于为什么在C ++中使用元组不是更常见?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆