efficiency

Generate lambdas for clarity and performance--Björn Fahller

A nice way to avoid code duplication and gain in readability:

Generate lambdas for clarity and performance

by Björn Fahller

From the article:

Higher order functions, functions that operate on other functions or returns functions, are familiar to those who have had some experience with functional programming, but they often seems magical to those who have not. Some of those with experience of using higher order functions have a gut feeling that they are expensive to use and prefer to avoid them...

Sorting by indices, part 2—Raymond Chen

It seems a simple problem, yet…

Sorting by indices, part 2

by Raymond Chen

From the article:

Before we dig into the Schwartzian transform, let's look at a more conventional generic way to sort by a key:

template<typename Iter, typename UnaryOperation, typename Compare>
void sort_by(Iter first, Iter last, UnaryOperation op, Compare comp)
{
  std::sort(first, last,
            [&](T& a, T& b) { return comp(op(a), op(b)); });
}

The idea here is that you give a unary operator op that produces a sort key, and we sort the items by that key according to the comparer...

Applying a permutation to a vector, part 1--Raymond Chen

It seems a simple problem, yet...

Applying a permutation to a vector, part 1

by Raymond Chen

From the article:

Suppose you have a vector indices of N integers that is a permutation of the numbers 0 through N − 1. Suppose you also have a vector v of N objects. The mission is to apply the permutation to the vector. If we let v2 represent the contents of the vector at the end of the operation, the requirement is that v2[i] = v[indices[i]] for all i...