我在这里想要做的是比较下面这个结构的两个列表。如果两个人分享至少3个特点,他们应该配对在一起并列入配对列表。我从名单上的第一个女孩开始,并将其与男孩进行比较,如果发现一对女孩,他们会把他们放在一个pairlist并从他们各自的boylist/girllist中删除。比较两个std :: lists的内容
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%,我得到一个错误消息说“列表迭代器不可defeferancable”。我有谷歌的错误消息,但我不知道该怎么做。也许我在想完全错误,或者可以以更好的方式完成,我不知道,但任何反馈意见。
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++;
}
}
}
标准库为您提供帮助您匹配容器内容的功能。看[这里](http://www.cplusplus.com/reference/algorithm/)。 –
http://stackoverflow.com/questions/596162/can-you-remove-elements-from-a-stdlist-while-iterating-through-it – doctorlove