Link to home
Start Free TrialLog in
Avatar of schneiderit
schneideritFlag for United Kingdom of Great Britain and Northern Ireland

asked on

combining two vectors

Hi Guys,

What's the best way to combine the contents of  two vectors like so:

If value in v1 is found in v2, add together values and insert into v3.

if value in v1 is not found in v2 insert v1 value into v3.

if value in v2 is not found in v1 insert v2 value into v3.

Many thanks.

- Lee.
Avatar of Infinity08
Infinity08
Flag of Belgium image

>> add together values

What do you mean by that ? If the value from v1 is found in v2, then that means that the values are the same. Do you mean to double that value and store it in v3 ? Or do you simply mean that you want v3 to be the union of v1 and v2 ?

I assume it's the latter, and in that case, take a look at the STL set_union algorithm :

        http://www.cplusplus.com/reference/algorithm/set_union.html
Avatar of schneiderit

ASKER

sorry, more detail.

struct Level
{
   int price_;
   int volume_;
}

The vectors contain level objects as above.  if the vectors contains level objects that have matching price, I need to add the volumes together and insert a new level object into  third vector.

if a level object exists in v1 with a price that the level objects in v2 don't have, then insert v1 object into v3.

if a level object exists in v2 with a price  that the level objects in v 1 don't have, then insert v2 object into v3.

Hope this is clearer.

Thanks.

- Lee.
>> if the vectors contains level objects that have matching price, I need to add the volumes together

That's a bit different than what I understood from your first post ;)

First, instead of a vector of structs, consider using a map, so you can link the price and volume together more easily (see below).
Then you can simply insert all elements from v1 into v3. After that, start adding the elements from v2 into v3, but check for duplicates - if a duplicate is found, simply add the two volumes.

typedef int Price;
typedef int Volume;
std::map<Price, Volume> v1;

Open in new window

Tthe objects need to be stored based on index position, not key. We are unable to use a map for this unfortunately.
>> We are unable to use a map for this unfortunately.

Then you'll just have to do a bit more work yourself :) Since apparently you cannot sort the data either ... (because that would change the order)

Which makes me wonder, how do you know what order the levels have to be in v3 ?
The order of levels in v3 doesn't matter, just wondered if there was a nice way of consolidating the two.  
>> The order of levels in v3 doesn't matter

Ok. Can you sort the data in v1 or v2 too ? Because that would make things easier.
No not currently,  but I could add a sort function.
>> No not currently,  but I could add a sort function.

STL already has a sort function. What I meant is : are you allowed to sort one of the two vectors ? Which will change its order ...
ASKER CERTIFIED SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
that should do it,  thanks.