0

My code right now:

#include <algorithm>
#include <iterator>
#include <string>
#include <vector>
using namespace std;

void FindIntersection(vector<vector<string>>& data)
{
    while (data.size() > 1)
    {
        auto lastVec = data.size() - 1;
        vector<string> result;
        std::set_intersection(data[0].begin(), data[0].end(),
                              data[lastVec].begin(), data[lastVec].end(),
                              std::back_inserter(result));
        data[0] = result;
        data.resize(lastVec);
    }
}

This narrows down the input to a single vector, which I then access using data[0]. The code works, but I don't like the assignment data[0] = result;, since vector copying, especially of string is not the most efficient. Any ideas how to improve this code?

P.S. I know that this optimization will have no real world benefit right now, but I am just curious to see how much better this can get.

Eyal K.
  • 1,042
  • 8
  • 23

0 Answers0