If I understand you correctly, not only your code but your approach is drastically wrong. You are trying to solve a connected components/disjoint sets type problem, but your method for instance only returns a single vector
of int
s...? It needs to return a vector
of vector<int>
s surely.
The following code is the closest thing to yours I can think of which should work. It should leave result
with the output you want.
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]);
}
}
Edit: Fixed a bug.