2013-08-05 70 views
1

我在这里想要做的是比较下面这个结构的两个列表。如果两个人分享至少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++;    
    } 




} 

}

+0

标准库为您提供帮助您匹配容器内容的功能。看[这里](http://www.cplusplus.com/reference/algorithm/)。 –

+0

http://stackoverflow.com/questions/596162/can-you-remove-elements-from-a-stdlist-while-iterating-through-it – doctorlove

回答

0

迎你到底有没有这个

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 ... 
{   
    i++;    
} 

在第二个如果,你不检查是否i!=end故能,所以i->intrests很可能会出现问题。 试试这个

if((i!=end) && number_of_intrests(i->intrests) <least_number_of_intrests)//number_of_intrests ... 
{   
    i++;    
} 
+0

如果我不会错过程序输出,那么这个窍门就是了。 – Torsten

0

你从你的列表在遍历它们,其迷惑迭代器擦除。相反,请复制你的清单。迭代原件,但从副本中删除。完成后,丢弃原件并保留副本。

编辑:你不必复制;你对你重置你的'我'迭代器的方式是正确的:它是安全的。但是当你从列表中删除时,你需要为'结束'变量设置一个新值。

+0

我们正在做i = girllist-> erase(i);虽然 – doctorlove

+0

因此,由于“列表:只有迭代器和对已擦除元素的引用无效[23.2.2.3/3]”请参阅http://stackoverflow.com/questions/6438086/iterator-invalidation-rules这不是问题,当然? – doctorlove

+0

我使用了你发布doctorlove的if语句,该程序似乎现在正在工作。 – Torsten