f



subtracting std::vector objects

Hi,

I have two vector<int> objects with the values 1,2,3,4,5,6 and 1,2,4,6
respectfully. Is there a function available within std which will give
me a vector<int> object with 3, 5 (subtraction).

Thanks,

Barry
0
1/15/2009 2:38:27 PM
comp.lang.c++ 49423 articles. 7 followers. Post Follow

5 Replies
276 Views

Similar Articles

[PageSpeed] 1

> I have two vector<int> objects with the values 1,2,3,4,5,6 and 1,2,4,6
> respectfully. Is there a function available within std which will give
> me a vector<int> object with 3, 5 (subtraction).

If you use std::set instead of vector, then you've got
std::set_difference
0
1/15/2009 2:43:56 PM
> Hi,
>
> I have two vector<int> objects with the values 1,2,3,4,5,6 and 1,2,4,6
> respectfully. Is there a function available within std which will give
> me a vector<int> object with 3, 5 (subtraction).

That's not subtraction.  And, as far as I know, there is nothing in
the
standard library that will do exacly that.

But you can use std::tranform to loop over the first vector and output
(via std::back_inserter) the resultant vector.  Just write a functor
that uses std::find to search the second vector.

HTH
0
AnonMail2005 (245)
1/15/2009 2:48:19 PM
> std::set_difference has nothing to do with std::set. It only requires that iterator ranges represent a set (no duplicated values) sorted in ascending order

Of course.
Sorry, that was temporary blackout :).
0
1/15/2009 3:05:05 PM
Igor R. wrote:
>> I have two vector<int> objects with the values 1,2,3,4,5,6 and 1,2,4,6
>> respectfully. Is there a function available within std which will give
>> me a vector<int> object with 3, 5 (subtraction).
> 
> If you use std::set instead of vector, then you've got
> std::set_difference

std::set_difference has nothing to do with std::set. It only requires 
that iterator ranges represent a set (no duplicated values) sorted in 
ascending order; which is already the case here. If it is not the case, 
std::sort and std::unique will do the job.

The OP can directly use std::set_difference on his data.

-- 
Michael
0
1/15/2009 3:06:13 PM
AnonMail2005@gmail.com wrote:
>> Hi,
>>
>> I have two vector<int> objects with the values 1,2,3,4,5,6 and 1,2,4,6
>> respectfully. Is there a function available within std which will give
>> me a vector<int> object with 3, 5 (subtraction).
> 
> That's not subtraction.

IMO The OP confused subtraction and difference.

> And, as far as I know, there is nothing in
> the standard library that will do exacly that.

std::set_difference does.

-- 
Michael
0
1/15/2009 3:16:02 PM
Reply: