【问题标题】:Comparing contents of two std::lists比较两个 std::lists 的内容
【发布时间】:2013-08-05 14:24:00
【问题描述】:

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




}

}

【问题讨论】:

标签: c++ list compare


【解决方案1】:

到最后你有这个

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

在第二个 if 中,您不检查 i!=end 是否可以,因此 i-&gt;intrests 很可能会导致问题。 试试这个

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

【讨论】:

  • 如果我没有看错程序输出,这就是诀窍。
【解决方案2】:

您在迭代列表时会从列表中删除,这会使迭代器感到困惑。相反,请复制您的列表。迭代原件,但从副本中删除。完成后,丢弃原件并保留副本。

编辑:您不必复制;您重置“i”迭代器的方式是正确的:它是安全的。但是,当您从列表中删除时,您确实需要为“end”变量设置一个新值。

【讨论】:

  • 那么,既然“列表:只有迭代器和对已擦除元素的引用无效 [23.2.2.3/3]”参见stackoverflow.com/questions/6438086/iterator-invalidation-rules,这肯定不是问题吗?
  • 我使用了您发布的 doctorlove 的 if 语句,该程序现在似乎正在运行。
  • @doctorlove 但是在erase()之后,'end'也失效了。所以你要离开你的名单了。我想你可以在删除女孩条目时重置“结束”。
  • @Torsen 我正在仔细检查擦除可能会使哪些内容无效。我已取消删除它,但可能有更简洁的方式来编写您的代码
  • @GreatBigBore 你确定,对于列表?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-30
相关资源
最近更新 更多