Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

combining two vectors

Posted on 2008-10-06
11
1,276 Views
Last Modified: 2012-06-27
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.
0
Comment
Question by:schneiderit
  • 6
  • 5
11 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 22652880
>> 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
0
 

Author Comment

by:schneiderit
ID: 22653016
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.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 22653134
>> 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

0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:schneiderit
ID: 22653179
Tthe objects need to be stored based on index position, not key. We are unable to use a map for this unfortunately.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 22653199
>> 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 ?
0
 

Author Comment

by:schneiderit
ID: 22653259
The order of levels in v3 doesn't matter, just wondered if there was a nice way of consolidating the two.  
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 22653281
>> 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.
0
 

Author Comment

by:schneiderit
ID: 22653348
No not currently,  but I could add a sort function.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 22653370
>> 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 ...
0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 125 total points
ID: 22653574
What I had in mind was something like :

#include <vector>
#include <algorithm>
 
typedef struct Level {
  int price;
  int volume;
 
  bool operator<(const Level &l) { return (this->price < l.price); }         // <-- defines the order needed for sorting
} Level;
 
void merge(std::vector<Level> &result, std::vector<Level> &v1, std::vector<Level> &v2) {
  result.clear();
  sort(v1.begin(), v1.end());
  sort(v2.begin(), v2.end());
 
  std::vector<Level>::iterator it1, it2;
  for (it1 = v1.begin(), it2 = v2.begin(); (it1 != v1.end()) && (it2 != v2.end());) {
    if (*it1 < *it2) { result.push_back(*it1++); }
    else if (*it2 < *it1) { result.push_back(*it2++); }
    else { result.push_back(*it1++); result.back().volume += (*it2++).volume; }
  }
  while (it1 != v1.end()) { result.push_back(*it1++); }
  while (it2 != v2.end()) { result.push_back(*it2++); }
}

Open in new window

0
 

Author Closing Comment

by:schneiderit
ID: 31503538
that should do it,  thanks.
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question