Hi,

I need to find the 'length' of union between 2 vectors.
The code I have is

vector<int> v1,v2;
	vector<int> unionsetv;
	vector<int>::iterator it;
        v1.push_back(1);v1.push_back(2);v1.push_back(3);
	v2.push_back(3);v2.push_back(2);v2.push_back(4);
        it = set_union(v1.begin(),v1.end(),v2.begin(),v2.end(),back_inserter(unionsetv));
        cout << unionsetv.size()<< " ";
        cout << int(it - unionsetv.begin())<< " " ;

I could not get this to work and I have used the set_union in the same way as given in the cplusplus.com reference. If I remove the iterator 'it' then I get the entire size of the unionsetv vector (here this is 6) but this will have zero values in it (obtained during union). What I need is a union where the length of union is 4 and not 6. In any case, I did not get my code to run. Please help me identify what I have overlooked.

Quote from cplusplus.com
For the function to yield the expected result, the elements in the ranges shall be already ordered according to the same strict weak ordering criterion (operator< or comp).

Oh yes. Infact the v1 and v2 vectors are sorted. It is just that in the code I gave here I did not put the exact way v1 and v2 are populated. But they are sorted before I call the set_union function. And I still do not get the length of the union. Please advise.

If you want help with your code, you have to post the code that is having problems. You can't expect people to figure out what's wrong by looking at different code.

Yes. Here is the simplied version of the code I use.

vector<int> v1,v2;
vector<int> unionsetv;
vector<int>::iterator it;
v1.push_back(1);v1.push_back(2);v1.push_back(3);
v2.push_back(2);v2.push_back(3);v2.push_back(4);
it = set_union(v1.begin(),v1.end(),v2.begin(),v2.end(),back_inserter(unionsetv));
cout << unionsetv.size()<< " ";
cout << int(it - unionsetv.begin())<< " " ;

If I do not use 'it', then unionsetv.size() gives 6. I hope I am clearer now. Please help.

Ok I've think I figured out why your code doesn't work, but I don't think there's a way to do it exactly like you want to.

The problem is this:
The return type of union_set is the same type as given for the 5th argument, the OutputIterator.
In your case this means union_set returns a back_inserter_iterator<vector<int>> yet you want to assign this to a vector<int>::iterator.

The easiest way to fix this is by indeed removing the it = and simply using unionsetv.end() whenever you would've wanted to use it.

When I try to run your code, it fails because "it" has the wrong time. When I remove the erroneous assignment to "it" in line 6, and the statement that tries to use "it" in line 8, the code prints 4 (correctly), not 6.

So if your code is printing 6, that means that what you're running is different from what you posted. I am not going to waste any more time by trying to read your mind.

Thank you Insensus and Arkoenig for your time. It is exactly the same code I used and no different but I am not sure why I keep getting 6 instead of 4. Meanwhile, I have done as Insensus has mentioned and now it works. :)
Thank you again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.