[Solved] Ignoring the Last String in Vectors of Vectors for set_difference


Assumptions:

using Lines = std::vector<std::vector<std::string>>;

Lines current = { ... }; // sorted on std::less<std::vector<std::string>>
Lines input = { ... }; // also sorted on std::less<std::vector<std::string>>

Lines difference;

Rather than doing

std::set_difference(current.begin(), current.end(), input.begin(), input.end(), std::back_inserter(difference));

You should do

auto compare = [](Lines::const_reference lhs, Lines::const_reference rhs)
{
    assert(lhs.size() && rhs.size())
    // same as default comparison, except skiping the last element
    return std::lexicographical_compare(lhs.begin(), std::prev(lhs.end()), rhs.begin(), std::prev(rhs.end()));
};
std::set_difference(current.begin(), current.end(), input.begin(), input.end(), std::back_inserter(difference), compare);

1

solved Ignoring the Last String in Vectors of Vectors for set_difference