Set_Union给出运行时错误

时间:2014-03-16 14:15:28

标签: c++ set set-union

我一直在研究一个问题,我要把n组的联合起来看看是否所有的组都是不相交的。我正在使用

set_union

我的代码草稿如下:

vector<int> I; // Vector to store union of all sets
vector<vector<int> >A; // Vectors whose union are to be taken
/* Read A */
repeat i from 0 to n-1
{
    I_size=I.size();
    Ai_size=A[i].size();
    I.resize(I_size+Ai_size);
    I=vector<int>(I.begin(),set_union(I.begin(),I.end(),A[i].begin(),A[i].end(),I.begin()));
    if(*(--I.end())==0) // If two sets contain some common term, last element of I will be 0
        Notify that the sets cannot be united // Break from the loop in case any two sets have common terms
}

如果I的最后一个元素为0,我将从循环中断开,因为A中的所有向量都不会包含0。 上面的代码不起作用。它给出了一些未知的运行时错误请告诉我哪里出错?

实际代码:

vector<vector<int> >A;
int n;

int SetIntersection()
{
    vector<int> I;
    for(int i=0; i<n; i++)
    {
        int I_size=I.size();
        int Ai_size=A[i].size();
        I.resize(I_size+Ai_size);
        I=vector<int>(I.begin(),set_union(I.begin(),I.end(),A[i].begin(),A[i].end(),I.begin()));
        if(*(--I.end())==0)
            return 0;
    }
return 1;
}

int main()
{
        int x;
        cin>>n;
        for(int i=0; i<n; i++)
        {
            cin>>x;
            A[i].push_back(x);
        }
        if(SetIntersection()==1)
            cout<<"Sets are disjoint";
        else
            cout<<"Sets are not disjoint";
return 0;
}

0 个答案:

没有答案
相关问题