954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

C++ set union help

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.

Sandhya212
Light Poster
27 posts since Jul 2009
Reputation Points: 10
Solved Threads: 1
 

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).

Insensus
Junior Poster
112 posts since Mar 2011
Reputation Points: 70
Solved Threads: 46
 

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.

Sandhya212
Light Poster
27 posts since Jul 2009
Reputation Points: 10
Solved Threads: 1
 

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.

arkoenig
Master Poster
703 posts since Jun 2010
Reputation Points: 359
Solved Threads: 109
 

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.

Sandhya212
Light Poster
27 posts since Jul 2009
Reputation Points: 10
Solved Threads: 1
 

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> yet you want to assign this to a vector::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.

Insensus
Junior Poster
112 posts since Mar 2011
Reputation Points: 70
Solved Threads: 46
 

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.

arkoenig
Master Poster
703 posts since Jun 2010
Reputation Points: 359
Solved Threads: 109
 

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.

Sandhya212
Light Poster
27 posts since Jul 2009
Reputation Points: 10
Solved Threads: 1
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: