1006. Join two lists

Description


list<int> join(const  list<int> &a, const list<int> &b)

//returns a list of distinct integers both in a and b. For example, a = (3,4,3,5); b = (2,3,2,12,4,4), then c = (3,4). or c = (4,3).  The order doesn't matter.
{

//insert here.

 

}


求两个列表中公共的元素(不重复),注意迭代器为const型。

#include <iostream>
#include <list>
using namespace std;

list<int> join(const  list<int> &a, const list<int> &b)
{
	list<int> c;
	for (list<int>::const_iterator a1 = a.begin();a1 != a.end();++a1)
	{
		for (list<int>::const_iterator b1 = b.begin();b1 != b.end();++b1)
		{
			if (*a1 == *b1)
			{
                //判断是否为重复元素
				int count = 0;
				for (list<int>::const_iterator c1 = c.begin();c1 != c.end();++c1)
				{
					if (*a1 == *c1)
					{
						count++;
					}
				}
				if (count == 0)
					c.push_back(*a1);
			}
		}
	}
	return c;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值