📄 subject_52572.htm
字号:
<p>
序号:52572 发表者:Rikcuo Akira 发表日期:2003-09-12 10:25:06
<br>主题:union problem
<br>内容:執行結果:<BR><BR>rolekind = 1 speed = 1 tmp1 = 1077936128 tmp2 = 1077936128 tmp3 = 3<BR>rolekind = 2 speed = 2 tmp1 = 1077936128 tmp2 = 1077936128 tmp3 = 3<BR>rolekind = 3 speed = 3 tmp1 = 1082130432 tmp2 = 1082130432 tmp3 = 4<BR>after sort<BR>rolekind = 3 speed = 3<BR>rolekind = 2 speed = 2<BR>rolekind = 1 speed = 1<BR>Press any key to continue<BR><BR>為何設定tmp3後tmp1,tmp2的數值會改變?<BR><BR><BR>////Source code<BR><BR>// test2.cpp : Defines the entry point for the console application.<BR>//<BR><BR>#include <iostream><BR>#include <list><BR><BR>using namespace std;<BR> <BR>class role<BR>{<BR>public:<BR><BR> int speed;<BR> int rolekind;<BR> typedef union<BR> {<BR> int tmp1;<BR> long tmp2;<BR> float tmp3;<BR> } type_mix;<BR><BR> type_mix foot;<BR><BR> role(){};<BR> <BR> role(int _rolekind,int _speed,int _foot)<BR> {<BR> rolekind=_rolekind;<BR> speed = _speed;<BR> foot.tmp1=_foot;<BR> foot.tmp2=_foot+1;<BR> foot.tmp3=_foot+2;<BR> };<BR><BR> bool operator == (const role& x) const <BR> {<BR> return rolekind==x.rolekind;<BR> }<BR><BR> bool operator <(const role& x) const<BR> {<BR> return speed > x.speed;<BR> };<BR><BR>};<BR><BR>template <class T><BR>class mycomp<BR>{<BR>public:<BR><BR> enum cmp_mode { normal,reverse};<BR><BR> cmp_mode mode;<BR><BR> mycomp(cmp_mode m=normal):mode(m)<BR> {<BR> }<BR><BR> bool operator() (const T& t1,const T& t2) const <BR> {<BR> if (mode==normal)<BR> return t1.speed < t2.speed ;<BR> else<BR> return t1.speed > t2.speed;<BR> }<BR>};<BR><BR>class mysort<BR>{<BR>public:<BR> <BR> bool operator() (const role& x, const role& y) const <BR> {<BR> return x < y;<BR> }<BR>};<BR><BR>bool test(const role& x,const role& y) <BR>{<BR> return x < y;<BR>};<BR><BR>template<class _Ty><BR> struct greater2 : binary_function<_Ty, _Ty, bool> {<BR> bool operator()(const _Ty& _X, const _Ty& _Y) const<BR> {return (_X > _Y); }<BR> };<BR><BR>template<> <BR>bool greater<role>::operator()(const role &r1,const role &r2) const<BR>{<BR> return r1.speed>r2.speed;<BR>};<BR><BR>class greater3 : public std::greater<role><BR>{<BR>public:<BR> bool operator() (const role& x, const role& y) const<BR> {<BR> return x.speed > y.speed;<BR> }<BR>};<BR><BR><BR>int main(int argc, char* argv[])<BR>{ <BR> std::list<role> role_list;<BR><BR> role_list.push_back(role(1,1,1));<BR> role_list.push_back(role(2,2,1));<BR> role_list.push_back(role(3,3,2));<BR><BR> std::list<role>::iterator pos;<BR><BR> for (pos=role_list.begin();pos!=role_list.end();++pos)<BR> {<BR> role a=*pos;<BR><BR> cout << "rolekind = " << pos->rolekind <BR> << " speed = " << pos->speed <BR> << " tmp1 = " << pos->foot.tmp1 <BR> << " tmp2 = " << pos->foot.tmp2 <BR> << " tmp3 = " << pos->foot.tmp3 <BR> << endl;<BR> }<BR> <BR> role_list.sort(greater3()); <BR><BR> //role_list.sort() 以operator<為準則,對所有元素排列<BR> //role_list.sort(op) 我想以op為準則,對所有元素排列的話,op函數的形式要如何寫??<BR><BR> cout << "after sort " << endl;<BR><BR> for (pos=role_list.begin();pos!=role_list.end();++pos)<BR> {<BR> cout << "rolekind = " << pos->rolekind <BR> << " speed = " << pos->speed << endl;<BR><BR> }<BR><BR> return 0;<BR>}<BR><BR>
<br><a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p>
<hr size=1>
<blockquote><p>
回复者:a dog 回复日期:2003-09-12 11:39:41
<br>内容:union在某一时刻只能存放一个值,<BR>设定tmp3後tmp1,tmp2的值就会丢失。
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
<font color=red>答案被接受</font><br>回复者:飞翔的瞳眸 回复日期:2003-09-12 11:43:59
<br>内容:union表示所有成员共用一块内存,你对任何成员赋值就是改写这一地址上的内容.当然所有成员的值都会相应的变化.<BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -