Return integers from vector intersection.

OPEN

hey guys, I am trying to return integers from a vector intersection I set up to give me the number of matching elements in two strings.

that works, now what I want to do is also return the actual integers that matched.

How can I take this code:

vector<string> intersection(vector<string> &v1, vector<string> &v2)
{
  vector<string> v3;

  sort(v1.begin(), v1.end());
  sort(v2.begin(), v2.end());

  set_intersection(v1.begin(),v1.end(),v2.begin(),v2.end(),back_inserter(v3));

  return v3;
}

int match_array()
{
  std::vector<string> v1 root.get<int>("user_length", 0);
  std::vector<string> v2 root.get<int>("job_length", 0);

  auto v3 = intersection(v1, v2);

  for(string n : v3)
     std::cout << n << ' ';
}

and make it also return a string or array of the matched integers.

Posted Jan 10, 2016, 6:07 PM UTC · edited

26
raevnos•

What integers? You're working with vectors of strings. What's root?

1
IonRedOP•

The string is a bunch of integers. These are all being pulled in through a REST call wich gives me JSON, I then parse it out before I do this.

1
raevnos•

Ah. So why not use vectors of ints instead? std::stoi() is the function for converting between strings and integers.

2
IonRedOP•

Okay, so how would I go about an implementation of this?

1
raevnos•

std::transform would be a good start.

1
IonRedOP•

Any more thoughts here, still have not gotten this to work.

1