C++ 问题 list iterator not dereferencable

大佬们 我这哪里错了呀

#include<iostream>
#include<list>

using namespace std;
int intersection(list<int>L, list<int>P, int a[]);
int cou = 0;
int a[10];

int main()
{
    list<int>L;
    list<int>P;
    L.push_back(1);
    L.push_back(3);
    L.push_back(4);
    L.push_back(6);

    P.push_back(1);
    P.push_back(2);
    P.push_back(3);
    P.push_back(4);
    P.push_back(5);
    P.push_back(6);
    P.push_back(7);
    intersection(L, P,a);
    for(int j=0;j<cou;j++)
    {
        cout << a[j] << endl;

    }
    getchar();
}
int intersection(list<int>L,list<int>P,int a[ ])
{
    int i = 0;


    for (list<int>::iterator itrl = L.begin();;++itrl)
    {
        for (list<int>::iterator itrp = P.begin();*itrp<=*itrl; ++itrp)
        {if(*itrl=*itrp)
        {
            a[i] = *itrl;
            i++;
            cou++;

        }
        }
    }
    return cou;
}
c++

1个回答

for (list<int>::iterator itrl = L.begin();;++itrl)//这句没有终止条件改为for (list<int>::iterator itrl = L.begin();itrl !=L.end();++itrl)
{
    for (list<int>::iterator itrp = P.begin();*itrp<=*itrl; ++itrp)
    {
            if(*itrl=*itrp)//这里是赋值,恒为true,不是条件判断,要加个=号
    {
        a[i] = *itrl;
        i++;
        cou++;

    }
    }
}
qq_37360437
やばい 谢谢大佬 写的时候没注意T_T
一年多之前 回复
立即提问