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

VC++
在类VC的界面实现中加入目录树
软件换肤技术在 BCB 中的实现
利用非模窗口生成MDI介面
报表输出轻松搞定
Windows 中不规则窗体的编程实现
解说Win32的窗口子类化
使用测试优先方法开发用户界面
一个简单的登录对话框的实现
一个简单的日记本程序
从资源中加载皮肤
一个在RichEdit中添加表情图象的类
ActiveSkin 4.3 软件换肤在VC中的实现
一种另类“关于(About)”对话框的动态显示方法
对话框打印预览及打印
关于如何换肤、子类化的解决方案
制作 MSN、QQ 的消息提示窗口
如何对 BCGControlBarPro 进行换肤
定制个性化的对话框窗口类
改变窗口中的光标形状
更新MFC中的视图,跟踪.NET Framework中的事件

VC++ 中的 Tuples


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