Click here to Skip to main content
14,021,693 members
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

1.6K views
Posted 14 Mar 2019
Licenced CPOL

Using Pointer to Members on STL Algorithms

, 14 Mar 2019
Rate this:
Please Sign up or sign in to vote.
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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

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

You may also be interested in...

Pro

Comments and Discussions

 
QuestionNo need for pointers here Pin
Jan Heckman16-Mar-19 1:23
professionalJan Heckman16-Mar-19 1:23 
AnswerRe: No need for pointers here Pin
thoughts-on-cpp16-Mar-19 4:12
memberthoughts-on-cpp16-Mar-19 4:12 
GeneralRe: No need for pointers here Pin
Jan Heckman16-Mar-19 6:59
professionalJan Heckman16-Mar-19 6:59 
PraiseGreat! Pin
koothkeeper15-Mar-19 10:13
professionalkoothkeeper15-Mar-19 10:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

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

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02 | 2.8.190417.4 | Last Updated 14 Mar 2019
Article Copyright 2019 by thoughts-on-cpp
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid