c++交集和并集问题。。。

[复制链接]
查看11 | 回复2 | 2008-11-29 22:44:02 | 显示全部楼层 |阅读模式
list& list::Intersect(const list& s) const
// Intersection
{

static list s_ret;
Node * temp = m_phead;

while (temp != NULL)

{

if (s.Find(temp->data) != -1)


{

s_ret.insert(temp->data);


}


temp = temp->next;

}


return s_ret;
}
这段代码是求交集的,但我不知道为什么要用static? 还有while (temp != NULL)

{

if (s.Find(temp->data) != -1)


{

s_ret.insert(temp->data);


}


temp = temp->next;

}
不知道是什么意思? 请大家教下我啊。。。谢谢
还有并集:
list& list::Union(const list& s) const //Union function
{
static list s_ret;
Node * temp = m_phead;
while (temp != NULL)
{
s_ret.insert(temp->data);
temp = temp->next;
}
temp = s.m_phead;
while (temp != NULL)
{
s_ret.insert(temp->data);
temp = temp->next;
}
return s_ret;
}
这2个while语句里的内容都是一样的,为什么要2次?谢谢。。。

回复

使用道具 举报

千问 | 2008-11-29 22:44:02 | 显示全部楼层
1、为什么要用static? 我估计是这样: s_ret这个变量(好像应该叫实例)是要return的,而在函数中定义的变量,在函数返回后,就不存在了,那返回就没有意义了。所以要加个static。 2、那个循环的意思就是访问本链表的所有结点,如果某个结点的数据不在s链表中,就将该数据插入s_ret中。 Node * temp = m_phead; //temp初值等于本链表首结点 while (temp != NULL) //当该结点存在的话{ if (s.Find(temp->data) != -1) //如果s链表中没有数据与temp所指结点中的数据相同{ s_ret.insert(temp->data)...
回复

使用道具 举报

千问 | 2008-11-29 22:44:02 | 显示全部楼层
static list s_ret的直如果不用静态下次进入这个函数的时候list s_ret的直又是个定义list s_ret的直 用了静态每次执行函数的时候list s_ret的直会保存改变后的直 while (temp != NULL) temp是指像头的 当头不为空的时候执行循环进入循环体内 并集的2个循环不一样用模版类类型指针在第一个循环的时候插入第...
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行