Tuples

2016-01-29 12:21 0 1 收藏

Tuples,Tuples

【 tulaoshi.com - C语言心得技巧 】

Tuples
作者:陶章志


就像我上期所报道的一样,在2002十月标准会议上,两个库扩展作为标准库延深,而被通过。

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com)

1         是Doug Gregor’s提出的多态函数的object wrappers。

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com)

2         Jaakko Järvi's提出的tuple类型。

这两个都是直接来在Boost项目。(Boost项目是一个C++ libraries 集合)上次,我答应在这期和下一期将介绍这两个扩展的库,这个月,就让我来简单的介绍一下tuple类型。

Tuple Types:一个简单Motivating例子

假如你想用一个函数返回多于一个返回值,例如:

// yields a quotient only
//
int IntegerDivide( int n, int d ) {
  return n / d;
}
 
// Sample use:
cout << "quotient = " << IntegerDivide( 5, 4 );
 
  在这个实现中有什么错误吗? 也许没有,毕竟在编译器中,我们内嵌了整数除法。包括结果也能够四舍五入。
  但是,如果我们想做更多。特别,想提供一个方法得到除法其他的信息,例如除法的余数。如果没有改变函数的结构。那么,实现这样的要求的函数不是一件容易的事情。
一种办法我们在函数中加入一个输出变量。
// Example 1(b): Integer division,
// yielding a quotient and remainder,
// one as the return value and one via
// an output parameter
//
int IntegerDivide( int n, int d, int& r ) {
  r = n % d;
  return n / d;
}
// Sample use:
int remainder;
int quotient = IntegerDivide( 5, 4, remainder );
cout << "quotient = " << quotient
<< "remainder = " << remainder;
 
这个方法的实现比较,但是我们经常这么实现。这种通过返回值和输出变量来返回函数返回值的办法,看起来有点不可思议。有人也许会说下面的办法更好。
// Example 1(c): Integer division,
// yielding a quotient and remainder,
// this time via two output parameters
//
void IntegerDivide( int n, int d, int& q, int& r ) {
  r = n % d;
  q = n / d;
}
 
// Sample use:
int quotient, remainder;
IntegerDivide( 5, 4, quotient, remainder );
cout << "quotient = " << quotient
    << "remainder = " << remainder;
这种办法也许更加协调。但是 还是比较含糊,不令人满意。稍微想一想,我们会记得为什么:Ralph Waldo Emerson建议我们:“一个愚笨的一致性的想法是思想混乱的怪物”(a foolish consistency is the hobgoblin of little minds)。这个版本能够正常工作,但是,如果你认为它不稳定的话,我不会责怪你。
那么该怎么做呢?在这一点我们通常会想起在标准库中我们有一个工具:std::pair,毕竟在标准模板库中有很多函数可以返回几个值 ,iterator范围就是作为一个单独的值-同时,大多通过pair<iterator,iterator实现的,同样的方法能够运行,如下:
// Example 1(d): Integer division,
// yielding a quotient and remainder,
// this time both in the return value
//
std::pair<int,int IntegerDivide( int n, int d ) {
  return pair<int,int( n/d, n%d );
}
 
// Sample use:
pair<int, int quot_rem = IntegerDivide( 5, 4 );
cout << "quotient = " << quot_rem.first
    << "remainder = " << quot_rem.second;
可以看出这是一个满意的做法,同时,它还可以提高。
Tuples in Action
  一些语言,包括Haskell, ML, 以及Python,都直接支持tuple types。C++不是这样,这是因为C++是一个能做任何事情,和内建标准库的系统语言,因此,我们能够,以库的形式实现我们自己的tuple types。像java等语言是把tuple type作为pair一个系列打包在一起。一个 tuple
type和“bundle-o-values”很相像。
在下面一个tuple-ized 的IntegerDivide例子和上面pair-ized 是很相像的,但是,我们不要被迷惑了,毕竟它使用的是一种新的方法:
// Example 2(a): Integer division,
// yielding a quotient and remainder,
// via a type return type
//
tuple<int,int IntegerDivide( int n, int d ) {
  return tuple<int,int( n/d, n%d );
}
 
// Sample use:
tuple<int, int quot_rem = IntegerDivide( 5, 4 );
cout << "quotient = " << quot_rem.get<0()
    << "remainder = " << quot_rem.get<1

来源:https://www.tulaoshi.com/n/20160129/1485707.html

延伸阅读

经验教程

840

收藏

29
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部