当前位置: 首页 > 图文教程 > 开发语言 > VC++ > Tuples

VC++
如何有效地使用对话框
一个定制CFileDialog对话框的实例
XP风格复活节彩蛋的实现
程序界面多模式显示的实现
改变视图单调的背景
使窗体拥有透明效果的API
《电子尺》V1.02程序开发实例
美化你的应用程序的外观界面
个人考勤软件开发实例
使用VC6.0实现窗口的任意分割
如何让一个打开的文档成为活动文档
创建非矩形窗口的简单方法
轻松实现类VC界面
视图的缩放的完整论述
如何获得另一个应用程序窗口中的文本
如何发送命令到文档对象
动画窗口的实现-VC++实例一则
如何在其他程序的窗口上创建按钮并使之能响应
如何在基于对话框的程序中动态设置鼠标指针
扩展COleDropTarget类来支持任意窗口拖放 - 作者:王加宝

VC++ 中的 Tuples


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-10-30   浏览: 39 ::
收藏到网摘: n/a

Tuples
作者:陶章志


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

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

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>