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

VC++
使用免费界面换肤软件 USkin
Cell插件在J2EE系统中的应用
采用MFC编制MVC模式之球体演示程序
托管资源全攻略
使用 MFC 编写打印程序
根据所选择的 TrueType 字体生成点阵数据
让你的软件界面更漂亮(四):不完美之菜单
VC界面的实现
让你的软件界面更漂亮(三)
分割窗口后如何限制分割条的移动范围
关于 CFileDialog 对话框多选功能的一个问题
让你的软件界面更漂亮(二)
对话框模板,RegexTest
让你的软件界面更漂亮(一)
利用窗口子类化隐藏系统图标
KVIP考勤系统
类似于FlashGet的悬浮框的制作
计算MDI子窗口数,仅显示文件夹的打开对话框
智能ABC窗口的实现
在打开文件对话框上实现图象预览

VC++ 中的 Tuples


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-10-30   浏览: 56 ::
收藏到网摘: 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>