Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/124.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 计算合并后剩余的向量数_C++_Algorithm_C++11_Stdvector_Union Find - Fatal编程技术网

C++ 计算合并后剩余的向量数

C++ 计算合并后剩余的向量数,c++,algorithm,c++11,stdvector,union-find,C++,Algorithm,C++11,Stdvector,Union Find,在这个问题中,我只使用std::vector,每个向量都是有序的,没有重复。现在我想合并具有相同数字的向量。因此,23可以与345并集,但不能与45或15并集 例如: 如果我有以下向量 1 1 2 3 4 5 1 5 2 4 7 在并集之后,我应该只剩下2个向量: 1 5 2 3 4 7 代码: vector<int> a,b,c,d,e,f,g; vector<vector<int>> myList; a.push_back(1); b.push_ba

在这个问题中,我只使用std::vector,每个向量都是有序的,没有重复。现在我想合并具有相同数字的向量。因此,23可以与345并集,但不能与45或15并集

例如:

如果我有以下向量

1
1
2 3 4
5
1 5
2
4 7
在并集之后,我应该只剩下2个向量:

1 5
2 3 4 7
代码:

vector<int> a,b,c,d,e,f,g;
vector<vector<int>> myList;

a.push_back(1);
b.push_back(1);
c.push_back(2);
c.push_back(3);
c.push_back(4);
d.push_back(5);
e.push_back(1);
e.push_back(5);
f.push_back(2);
g.push_back(4);
g.push_back(7);

myList.push_back(a);
myList.push_back(b);
myList.push_back(c);
myList.push_back(d);
myList.push_back(e);
myList.push_back(f);
myList.push_back(g);

//this should print out the vectors in my above example
for (int i =0; i<myList.size(); i++) {
    for (int j=0; j<myList[i].size(); j++) {
        cout<<myList[i][j]<<" ";
    }
    cout<<endl;
}
向量a、b、c、d、e、f、g;
矢量糜棱岩;
a、 推回(1);
b、 推回(1);
c、 推回(2);
c、 推回(3);
c、 推回(4);
d、 推回(5);
e、 推回(1);
e、 推回(5);
f、 推回(2);
g、 推回(4);
g、 推回(7);
我的列表。向后推(a);
我的列表。推回(b);
我的列表。推回(c);
我的列表。向后推(d);
我的列表。向后推(e);
我的列表。推回(f);
我的列表。推回(g);
//这应该打印出上面例子中的向量
对于(inti=0;i这不是完美的

vector<int> myResult(20);

myIt = set_intersection(myTemp.begin(), myTemp.end(), 
     myTemp2.begin(), myTemp2.end(), myResult.begin());
back\u inserter
使用
push\u back
将元素添加到向量中,因此myResult从大小为零开始,并根据交叉点中的元素数量以正确的大小结束。因此,您只需测试
myResult.size()>0
,即可判断是否存在任何公共元素


调用set_union时也要这样做。

如果我理解正确,不仅是您的代码,而且您的方法都大错特错。您试图解决连接组件/不相交集类型的问题,但您的方法例如只返回一个
int
s的
向量。?它需要返回一个
向量f
vector
s

下面的代码是我能想到的最接近您的代码,它应该会留下
result
和您想要的输出

vector< vector<int> > result;

for(int i = 0; i < myList.size(); i++)
{

    bool match = false;
    int matchFirst = -1;

    for(int j = 0; j < result.size(); j++)
    {

        vector<int> myResult;
        vector<int> myResult2;

        set_intersection(myList[i].begin(), myList[i].end(),
                         result[j].begin(), result[j].end(),
                         back_inserter(myResult));

        if (myResult.size())
        {
            set_union(myList[i].begin(), myList[i].end(),
                      result[j].begin(), result[j].end(), back_inserter(myResult2));

            if(match)
            {
                vector<int> myResult3;
                set_union(myResult2.begin(), myResult2.end(),
                          result[matchFirst].begin(), result[matchFirst].end(), back_inserter(myResult3));
                result.erase(result.begin() + j, result.begin() + j + 1);
                result[matchFirst] = myResult3;
                j--;
            }
            else
            {
                matchFirst = j;
                result[j] = myResult2;
                match = true;
            }

        }

    }

    if(!match)
    {
        result.push_back(myList[i]);
    }
}
vector结果;
对于(int i=0;i

编辑:修复了一个错误。

到目前为止,您有什么问题?它是如何不按预期工作的?set_union和set_intersection听起来是一个合理的开始。为什么不向我们展示不工作的代码,我们可以帮助您修复它。@Andy Rowl补充道,请看一看。@john补充道,请看一看。
#include <iterator>

vector<int> myResult;

set_intersection(myTemp.begin(), myTemp.end(), 
    myTemp2.begin(), myTemp2.end(), 
    back_inserter(myResult));
vector< vector<int> > result;

for(int i = 0; i < myList.size(); i++)
{

    bool match = false;
    int matchFirst = -1;

    for(int j = 0; j < result.size(); j++)
    {

        vector<int> myResult;
        vector<int> myResult2;

        set_intersection(myList[i].begin(), myList[i].end(),
                         result[j].begin(), result[j].end(),
                         back_inserter(myResult));

        if (myResult.size())
        {
            set_union(myList[i].begin(), myList[i].end(),
                      result[j].begin(), result[j].end(), back_inserter(myResult2));

            if(match)
            {
                vector<int> myResult3;
                set_union(myResult2.begin(), myResult2.end(),
                          result[matchFirst].begin(), result[matchFirst].end(), back_inserter(myResult3));
                result.erase(result.begin() + j, result.begin() + j + 1);
                result[matchFirst] = myResult3;
                j--;
            }
            else
            {
                matchFirst = j;
                result[j] = myResult2;
                match = true;
            }

        }

    }

    if(!match)
    {
        result.push_back(myList[i]);
    }
}