subtracting std::vector objects

M

magnus.moraberg

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
 
I

Igor R.

I have two vector said:
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
 
A

AnonMail2005

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
 
I

Igor R.

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

Michael DOUBEZ

Igor said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,817
Latest member
AdalbertoT

Latest Threads

Top