14,022,154 members
Technical Blog
alternative version

#### Stats

1.6K views
Posted 14 Mar 2019
Licenced CPOL

# Using Pointer to Members on STL Algorithms

, 14 Mar 2019
Using pointer to members is sometimes a mighty feature of C++. In this post you see a way of how to use them in STL algorithms.

Sometimes I need to execute an operation, like std::sort or std::transform, on a container of structs or objects using a lambda or function pointers which should take type defined criteria. Another scenario might be a global minima search of an arbitrary brane which could be described by a vector of 3-dimensional vectors. In such cases, we often use pointer to members.

This topic might be a piece of cake for every experienced C++ veteran. But I remember back in the days when I was a novice, I was really irritated by a piece of code which was using pointer to members, to do a special operation. I didn’t get what that piece of sh*t was doing and why it was written that wired. And even worse, it was neither easy to find documentation of that code, nor information of such “magic” it was doing on the internet.

So how might something like this look like? Let’s say we have a vector of complex numbers which we want to sort by either the real or the imaginary number. This is our complex type:

```struct Complex {
double real;
double imaginary;
};```

For sorting vectors, we would use std::sort of course and for vectors of primitive types, it’s rather simple to use. And indeed, it’s easy to use with complex types as well. Because std::sort is offering an interface which is taking a compare function object that has to fulfill the requirements of Compare:

```template<class RandomIt, class Compare>
constexpr void sort(RandomIt first, RandomIt last, Compare comp); //constexpr since C++ 20```

Using std::sort for sorting according to real or imaginary numbers might then look like this:

```std::vector<Complex> vec = { { 3, 1},
{ 1, 9 },
{ 8, 2 },
{ 3, 14 }};

double Complex::*var;
auto comparator = [&var](const Complex &a, const Complex &b) { return a.*var < b.*var; };

var = &Complex::real; //Sorting for real numbers
std::sort(vec.begin(), vec.end(), comparator);

var = &Complex::imaginary; //Sorting for imaginary numbers
std::sort(vec.begin(), vec.end(), comparator);```

The interesting point in the code above is the declaration of double Complex::*var (pointer to member of Complex of type double), in line 6, which is passed to the lambda over the captures clause. The lambda function is using the pointer by dereferencing it to a comparable type (in our case a double) in line 7. It’s then used by assigning it a concrete pointer in line 9 and 12 to steer the std::sort algorithm.

For me, such indirections are sometimes quite useful but not always. The biggest problem might be the readability for programming beginners. Also, there might be a better way to have the same functionality without pointers to members. I would be glad to hear any suggestions or feedback.

Did you like this post?

What are your thoughts on this post?

Feel free to comment and share the post.

## About the Author

 Team Leader KISSsoft AG Switzerland
Passionate C++ developer, mechanical engineer, Head of Software Development at KISSsoft AG and host of https://thoughs-on-cpp.com

 Pro

## Comments and Discussions

 First Prev Next
 No need for pointers here Jan Heckman16-Mar-19 1:23 Jan Heckman 16-Mar-19 1:23
 Re: No need for pointers here thoughts-on-cpp16-Mar-19 4:12 thoughts-on-cpp 16-Mar-19 4:12
 Re: No need for pointers here Jan Heckman16-Mar-19 6:59 Jan Heckman 16-Mar-19 6:59
 Great! koothkeeper15-Mar-19 10:13 koothkeeper 15-Mar-19 10:13
 Your explanation is concise and informative! Although I've been programming in C++ since 1991, the C++ language is fast passing me by as I'm now retired (since October 2018). I notice that you have recently posted several more articles and I look forward to reading them all. With more and more young programmers entering the field, and seasoned programmers adding content filled with their experience, there are plenty of opportunities to learn. Sincerely, Keith E. Cooper
 Last Visit: 18-Apr-19 8:38     Last Update: 18-Apr-19 8:38 Refresh 1

General    News    Suggestion    Question    Bug    Answer    Joke    Praise    Rant    Admin

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.