我在这里要做的是比较两个结构列表,如下面的这个。如果两个人至少共享 3 个兴趣,他们应该配对在一起并放入配对列表中。我从列表中的第一个女孩开始,并将其与男孩进行比较,如果找到一对,则将它们放入配对列表中,并将其从各自的男孩/女孩列表中删除。
struct Person {
char name[30];
enum gendertype gender;
TableOfIntrests intrests; //The tableofintrests consists of an array with 6 containters representing six diffrent intrests.
};
无论如何,我遇到的问题是该程序可能有大约 50% 的时间可以匹配人和创建配对。另外约 50% 我收到一条错误消息,上面写着“列表迭代器不可取消引用”。我有谷歌错误信息,但我不知道该怎么做。也许我的想法完全错误,或者可以以更好的方式完成,我不知道,但感谢任何反馈。
void pair_together(Personlist *girllist, Personlist *boylist, Pairlist *pairlist, int least_number_of_intrests)
{
int equal_intrests = 0;
Pair pair;
Person p, p2;
int testcount3=0;
std::list<Person>::iterator i = girllist->begin();
std::list<Person>::iterator end = girllist->end();
std::list<Person>::iterator i2 = boylist->begin();
std::list<Person>::iterator end2 = boylist->end();
while ((i != end))
{
testcount3=0;
if(i2==end2)
break;
equal_intrests = number_of_equal_intrests(i->intrests, i2->intrests); //number_of_equal_intrests return the number of intrests that the two persons shares.
if(equal_intrests >= least_number_of_intrests)
{
printf("%s + %s, ", i->name, i2->name);
printf("%d\n", equal_intrests);
equal_intrests =0;
create_person(&p, i->name, i->gender);
create_person(&p2, i2->name, i2->gender);
create_pair(&pair, p, p2);
pairlist->push_back(pair);
i =girllist->erase(i);
i2 =boylist->erase(i2);//--
i2=boylist->begin();
testcount3=1;
}
else if(testcount3!=1)
{
i2++;
}
if((i2==end2) && (equal_intrests < least_number_of_intrests))
{
i++;
i2=boylist->begin();
}
if(number_of_intrests(i->intrests) <least_number_of_intrests)//number_of_intrests returns how many intrests a person have, so if the person have less intrests than least_number_of_intrests the program just skips to the next person.
{
i++;
}
}
}