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;
}