很好用于工会 [英] Good use for Unions

查看:80
本文介绍了很好用于工会的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好我已经编程C ++很长一段时间了,但我从未使用过

工会。我试图找到一个可以使用它们的好例子。可以

任何人提供一个?

问候


迈克尔。

Hi guys I''ve been programming C++ for quite some time, but I have never used
unions. I am trying to find a good exmaple of when they might be used. can
anyone provide one??
Regards

Michael.

推荐答案

迈克尔写道:
大家好我已经编程C ++很长一段时间了,但我从来没有使用
工会。我试图找到一个可以使用它们的好例子。可以
任何人提供一个??
Hi guys I''ve been programming C++ for quite some time, but I have never used
unions. I am trying to find a good exmaple of when they might be used. can
anyone provide one??




查找VARIANT Microsoft的OLE和COM文档中的数据类型。


V



Look up "VARIANT" data type in Microsoft''s OLE and COM documents.

V


我想不出一个很常见的案件。但是,在一些递归数据结构(例如树)中,您可能希望每个概念节点都是

" real"节点或nill节点。实现

这样的数据结构的方法有很多种。假设你真的*关心间接

(即,你希望避免它)。然后你可以创建一个向量,其中每个元素都是

结构节点的对象。
{

bool m_real_node;


union m_true_node

{

real_node m_real_node;


nil_node m_nil_node;

}

};


所以你可以使用工会。据我了解,

以上(非常丑陋)的例子应该在非常极端的情况下使用(如果你确信这实际上会提高性能),b / b
,并且你应该把b / b
封装到一个容器中,使用这样的节点

内部。


顺便说一下,我模糊地回忆起上面的代码的工作速度是我没有工会写的那个版本的3倍〜(基于指针,并使用NULL来表示

a" nil节点) (当然,既然我把它们都写了,谁知道我可能会给b
捏造,也就是说,这已经好几年了,我不记得确切的设置了。) >
" Michael" < SL *********** @ hotmail.com>在消息中写道

news:ct ********** @ hercules.btinternet.com ...
I can''t think of a very common case. However, in some recursive data
structures (e.g., trees), you might want each conceptual node to be either a
"real" node or a "nill" node. There are many different ways of implementing
such a data structure. Suppose you''re *really* concerned about indirection
(i.e., you wish to avoid it). Then you might create a vector whose each
element an object of
struct node
{
bool m_real_node;

union m_true_node
{
real_node m_real_node;

nil_node m_nil_node;
}
};

So that''s a case where you might use a union. As far as I understand, the
above (quite ugly) example should be used in very extreme circumstances (if
you''re convinced this will actually improve performance), and you should
encapsulate the hell out of it into a container employing such nodes
internally.

By the way, I vaguely recall the above code working ~3 times as fast as a
version I wrote without unions (based on pointers, and using NULL to signify
a "nil" node) (of course, since I wrote them both, who knows what I might
have fudged, also, it''s been years, and I can''t recall the exact settings).
"Michael" <sl***********@hotmail.com> wrote in message
news:ct**********@hercules.btinternet.com...
大家好我编程C ++很长一段时间,但我从来没有使用过
的工会。我试图找到一个可以使用它们的好例子。可以
任何人提供一个吗?
问候

迈克尔。
Hi guys I''ve been programming C++ for quite some time, but I have never used unions. I am trying to find a good exmaple of when they might be used. can
anyone provide one??
Regards

Michael.





迈克尔写道:

Michael wrote:
大家好我已经编程了C ++很长一段时间了,但我有
从未使用过工会。我试图找到一个很好的例子,它们可能是
。任何人都可以提供一个吗?
问候

迈克尔。
Hi guys I''ve been programming C++ for quite some time, but I have never used unions. I am trying to find a good exmaple of when they might be used. can anyone provide one??
Regards

Michael.




强制对齐缓冲区:

union {

char buffer [1000];

int dummy;

};


这会强制编译器在整数边界处启动缓冲区。

-shez-



Force alignment of a buffer:

union {
char buffer[1000];
int dummy;
};

This forces the compiler to start ''buffer'' at an integer boundary.
-shez-


这篇关于很好用于工会的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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